Ticket #8148 (closed bug: fixed)
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.
Change History
comment:2 Changed 2 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:
comment:3 follow-up: ↓ 5 Changed 2 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() method. 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.
comment:5 in reply to: ↑ 3 Changed 2 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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

This is the cause of http://bugs.jqueryui.com/ticket/6918