Opened 10 years ago
Closed 8 years ago
#13428 closed bug (migrated)
focus event ignores additional data when triggered
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | low | Milestone: | None |
Component: | event | Version: | 1.9.1 |
Keywords: | Cc: | dmethvin | |
Blocked by: | #13353 | Blocking: |
Description
http://fiddle.jshell.net/rvX5Y/4/
$('input').on('focus', function(event, extra, data) { console.log(extra, data); }); $('input').trigger('focus', ['extra', 'data']);
Works in version 1.8.3.
Does not work in version 1.9.0 and 1.9.1.
Expected behavior: Console shows "extra data"
False Behavior: Console shows "undefined undefined"
Browser: Firefox 18, OS: OS X 10.8.2
Change History (17)
comment:1 Changed 10 years ago by
Blocked by: | 13353 added |
---|---|
Cc: | dmethvin added |
Component: | unfiled → event |
comment:2 Changed 10 years ago by
Way too much code though. I was thinking that if the user passed data that we use the "standard" trigger path rather than native events. It passes the data at the expense of some non-standard event behavior, but I think people are probably misusing the event system most of the time when this happens.
comment:3 Changed 10 years ago by
That'd probably be acceptable for focus
, but click
handlers would see different checked states on checkbox and radio inputs when the event was provided trigger data—a definite no-go.
comment:4 Changed 10 years ago by
Owner: | set to [email protected]… |
---|---|
Status: | new → pending |
So, to the OP, are you using this to initialize on page load? Could you use .triggerHandler()
instead, or are you really trying to focus the element as well?
comment:5 Changed 10 years ago by
Status: | pending → new |
---|
I was trying to distinguish between a) clicking or tabbing into the input field and b) using the .trigger('focus') function somewhere else. Nothing on page load.
comment:7 Changed 10 years ago by
Status: | new → open |
---|---|
Version: | git → 1.9.1 |
comment:9 Changed 10 years ago by
triggerHandler is not really an acceptable workaround since it won't actually focus, blur, etc...
comment:10 follow-up: 12 Changed 10 years ago by
It is a fine workaround. Just do the indicated DOM action afterwards.
var $x = $("#aninput"); $x.triggerHandler("focus"); $x[0].focus();
comment:11 Changed 10 years ago by
People from the core team certainly know this already, but this arises from:
focus: { // Fire native event if possible so blur/focus sequence is correct trigger: function() { if ( this !== safeActiveElement() && this.focus ) { this.focus();
and
blur: { trigger: function() { if ( this === safeActiveElement() && this.blur ) { this.blur();
comment:12 Changed 10 years ago by
Won't this trigger the handlers twice?
Replying to dmethvin:
It is a fine workaround. Just do the indicated DOM action afterwards.
var $x = $("#aninput"); $x.triggerHandler("focus"); $x[0].focus();
comment:13 Changed 10 years ago by
I also have this issue and it is still valid with latest 1.x and 2.x, here is a jsFiddle for what I'm doing: http://jsfiddle.net/2dVMa/
My problem with using triggerHandler is that I am trying to avoid running the code in my 'focus' handler when setting focus programatically (with 'trigger'). (Only workarounds I can think of would be unbinding, triggering focus and then rebinding, or reverting to 1.8... )
Others with same issue: http://forum.jquery.com/topic/extra-arguments-passed-to-trigger-doesn-t-reach-blur-event-handler-when-element-has-focus
Browser: Chrome 27, OS: OS X 10.8.4
comment:16 Changed 8 years ago by
Priority: | undecided → low |
---|
comment:17 Changed 8 years ago by
Resolution: | → migrated |
---|---|
Status: | open → closed |
Migrated to https://github.com/jquery/jquery/issues/1741
This is exactly analogous to #13353... I'm starting to think we should generalize and incorporate that solution after all.