Bug Tracker

Opened 9 years ago

Closed 8 years ago

#7131 closed bug (invalid)

Multi Selector + Non Existnig Context + Live to bind event handlers = event is bound to all but first part of the multi selector

Reported by: nhaemhou Owned by: danheberden
Priority: low Milestone: 1.next
Component: selector Version: 1.4.2
Keywords: live, multi selector, context, event Cc:
Blocked by: Blocking:

Description

Found a pretty annoying bug. I was expecting that when the context doesn't exist (i.e. not found on the current page), that live would not attach the event handlers to the elements in a multi selector. Turns out that it does it as expected for the first part of the multi selector and then ignores the non existing context for the other parts (i.e. it attaches the event handler)

Example:


<div class="foobar">

<div class="a">AAA</div> <div class="b">BBB</div> <div class="c">CCC</div>

</div>

var ctx = $('.nonExistingClass'); $('.a, .b, .c', ctx).live('click', function(event) {

$(this).css('color','red');

});

If the context doesn't exist, live still attaches the event handler to .b and .c If you use bind instead, it works correctly and none of the parts of the multi selector get the event handler.

var ctx = $('.nonExistingClass'); $('.a, .b, .c', ctx).bind('click', function(event) {

$(this).css('color','red');

});

Change History (4)

comment:1 Changed 9 years ago by addyosmani

Priority: undecidedlow
Status: newopen

I can confirm the behavior mentioned is experienced when using jQuery 1.4.2 or 1.4.3 RC2.

Flagging for a further review and possibly a patch if possible.

comment:2 Changed 9 years ago by snover

Milestone: 1.4.3

Resetting milestone to future.

comment:3 Changed 9 years ago by danheberden

Milestone: 1.next
Owner: set to danheberden
Status: openassigned

comment:4 Changed 8 years ago by john

Resolution: invalid
Status: assignedclosed

You should only be using an element as a context, not a jQuery set. Do this instead: var ctx = $('.nonExistingClass')[0];

Note: See TracTickets for help on using tickets.