Bug Tracker

Opened 7 years ago

Closed 7 years ago

#8573 closed bug (wontfix)

Using a jQuery selections function as an eventHandler confuses jQuery.

Reported by: kitsunde Owned by:
Priority: low Milestone: 1.next
Component: core Version: 1.5.1
Keywords: Cc:
Blocked by: Blocking:

Description

$("button").live("click", $("div").hide );

Results in an error: Chrome 11 dev: #<HTMLButtonElement> has no method 'animate' Firefox 3.6: this.animate is not a function

Indicating that the hide call is being executed on the "button" selection rather than the "div" selection.

Test case: http://jsfiddle.net/bLAvM/

I've also tested in jQuery 1.3.2 which gives the same problem, and I've attempted to use $("button").click instead of .live with the same exception.

Change History (1)

comment:1 in reply to:  description Changed 7 years ago by Rick Waldron

Component: unfiledcore
Priority: undecidedlow
Resolution: wontfix
Status: newclosed

Replying to kitsunde:

$("button").live("click", $("div").hide );

Results in an error: Chrome 11 dev: #<HTMLButtonElement> has no method 'animate' Firefox 3.6: this.animate is not a function

Indicating that the hide call is being executed on the "button" selection rather than the "div" selection.

This happens because jQuery manages scope for callbacks in an unorthodox, yet consistent manner and all it sees you passing is a function definition, to which it will coerce the scope into whatever the calling selector set was.

Test case: http://jsfiddle.net/bLAvM/

I've also tested in jQuery 1.3.2 which gives the same problem, and I've attempted to use $("button").click instead of .live with the same exception.

Note: See TracTickets for help on using tickets.