Bug Tracker

Opened 6 years ago

Closed 5 years ago

Last modified 4 years ago

#8148 closed bug (fixed)

Change event is never triggered if another element is programmatically focused

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

Description (last modified by scottgonzalez)

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 (7)

comment:1 Changed 6 years ago by scottgonzalez

  • Description modified (diff)

comment:2 Changed 6 years ago by rwaldron

  • Component changed from unfiled to event
  • Keywords needsreview added

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: Changed 6 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 6 years ago by dmethvin (next)

comment:4 Changed 6 years ago by rwaldron

#8169 is a duplicate of this ticket.

comment:5 in reply to: ↑ 3 Changed 6 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 5 years ago by dmethvin

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

Fixed in 1.5.2.

comment:7 Changed 5 years ago by dmethvin

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