Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 10 years ago

#10320 closed bug (invalid)

pass a jquery object as trigger data

Reported by: [email protected] Owned by:
Priority: low Milestone: None
Component: event Version: 1.6.4
Keywords: Cc:
Blocked by: Blocking:

Description

 $('#elt').bind('z', function( event, obj) {
  console.log(obj);
 });

 $('#elt').trigger('z', $('#elt'));

I would expect the handler will display a jquery object (actually, the one I passed as a parameter). Instead, it displays the DOM #elt object.

Change History (3)

comment:1 Changed 12 years ago by timmywil

Component: unfiledevent
Priority: undecidedlow
Resolution: invalid
Status: newclosed

additional parameters should be passed with an array.

$('#elt').trigger('z', [ $('#elt') ]);

But in this instance, it will be triggered in the context of #elt anyway.

comment:2 Changed 10 years ago by [email protected]

If this is the case, it is incorrectly documented in the API documentation ( http://api.jquery.com/trigger/) (emphasis mine):

"The event object is always passed as the first parameter to an event handler, but if additional parameters are specified during a .trigger() call, these parameters will be passed along to the handler as well. To pass more than one parameter, use an array as shown here. As of jQuery 1.6.2, a single parameter can be passed without using an array."

The functionality you describe does not work as intended, as a single param still requires an array to make it work. Here's a fiddle proving this: http://jsfiddle.net/edelman/vg68j/

comment:3 Changed 10 years ago by dmethvin

The docs are wrong here. It's never been a good idea to pass something other than an honest JavaScript Array, although it works for simple values like numbers. The problem with passing a jQuery object is that it looks array-like to makeArray, but it's no Array.

Issue created here: https://github.com/jquery/api.jquery.com/issues/178

Note: See TracTickets for help on using tickets.