Bug Tracker

Opened 7 years ago

Closed 5 years ago

#13428 closed bug (migrated)

focus event ignores additional data when triggered

Reported by: mail@… Owned by: mail@…
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 7 years ago by gibson042

Blocked by: 13353 added
Cc: dmethvin added
Component: unfiledevent

This is exactly analogous to #13353... I'm starting to think we should generalize and incorporate that solution after all.

comment:2 Changed 7 years ago by dmethvin

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 7 years ago by gibson042

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 7 years ago by dmethvin

Owner: set to mail@…
Status: newpending

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 7 years ago by mail@…

Status: pendingnew

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

#13590 is a duplicate of this ticket.

comment:7 Changed 7 years ago by timmywil

Status: newopen
Version: git1.9.1

comment:8 Changed 7 years ago by dmethvin

#13711 is a duplicate of this ticket.

comment:9 Changed 7 years ago by anonymous

triggerHandler is not really an acceptable workaround since it won't actually focus, blur, etc...

comment:10 Changed 7 years ago by dmethvin

It is a fine workaround. Just do the indicated DOM action afterwards.

var $x = $("#aninput");
$x.triggerHandler("focus");
$x[0].focus();

comment:11 Changed 7 years ago by anonymous

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 in reply to:  10 Changed 7 years ago by anonymous

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 6 years ago by osmestad

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

Last edited 6 years ago by osmestad (previous) (diff)

comment:14 Changed 6 years ago by dmethvin

#14658 is a duplicate of this ticket.

comment:15 Changed 6 years ago by dmethvin

#14829 is a duplicate of this ticket.

comment:16 Changed 5 years ago by m_gol

Priority: undecidedlow

comment:17 Changed 5 years ago by m_gol

Resolution: migrated
Status: openclosed
Note: See TracTickets for help on using tickets.