Bug Tracker

Ticket #8148 (closed bug: fixed)

Opened 3 years ago

Last modified 2 years ago

Change event is never triggered if another element is programmatically focused

Reported by: scott.gonzalez Owned by:
Priority: undecided Milestone: 1.next
Component: event Version: 1.5
Keywords: Cc:
Blocking: Blocked by:

Description (last modified by scott.gonzalez) (diff)

If the value of a text field is changed and then focus is programmatically moved to another element, the native change event is triggered, but any event handlers bound via jQuery will not be triggered.

 http://jsbin.com/elazu4/5/edit

Change History

comment:1 Changed 3 years ago by scott.gonzalez

  • Description modified (diff)

comment:2 Changed 3 years ago by rwaldron

  • Keywords needsreview added
  • Component changed from unfiled to event

Scott, I'm adding this to show that change is fired when the input receives focus again:

 http://jsbin.com/elazu4/7/

comment:3 follow-up: ↓ 5 Changed 3 years ago by dmethvin

OK, here's a bare-DOM example showing the order events are fired:

 http://jsbin.com/elazu4/8/edit

I think what is happening here is jQuery.event.trigger sets a jQuery.event.triggered flag just before calling the native DOM <a>.focus() event. The browser fires the blur and change events DURING that process, which normally would be handled but are blocked because the flag is checked in eventHandle:

 https://github.com/jquery/jquery/blob/master/src/event.js#L402

 https://github.com/jquery/jquery/blob/master/src/event.js#L85

It works properly if you start the process by using DOM <a>.focus() which calls the jQuery.event.handle directly, rather than $("a").focus() which calls jQuery.event.trigger. This appears to have been the way events were handled for several years afaict.

Version 0, edited 3 years ago by dmethvin (next)

comment:4 Changed 3 years ago by rwaldron

#8169 is a duplicate of this ticket.

comment:5 in reply to: ↑ 3 Changed 3 years ago by masklinn

Replying to dmethvin:

It works properly if you start the process by using DOM <a>.focus() which calls the jQuery.event.handle directly, rather than $("a").focus() which calls jQuery.event.trigger. This appears to have been the way events were handled for several years afaict.

The exact same issue can be seen by using .blur() on the node itself rather than .focus() on an other node: $(this).blur() will fail to trigger onchange() but this.blur() will correctly trigger it.

comment:6 Changed 3 years ago by dmethvin

  • Status changed from new to closed
  • Resolution set to fixed

Fixed in 1.5.2.

comment:7 Changed 3 years ago by dmethvin

  • Keywords needsreview removed
Note: See TracTickets for help on using tickets.