Skip to main content

Bug Tracker

Side navigation

#7131 closed bug (invalid)

Opened October 08, 2010 01:16PM UTC

Closed July 12, 2011 04:35PM UTC

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');

});

Attachments (0)
Change History (4)

Changed October 13, 2010 05:30AM UTC by addyosmani comment:1

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.

Changed November 12, 2010 02:40AM UTC by snover comment:2

milestone: 1.4.3

Resetting milestone to future.

Changed March 31, 2011 04:32AM UTC by danheberden comment:3

milestone: → 1.next
owner: → danheberden
status: openassigned

Changed July 12, 2011 04:35PM UTC by john comment:4

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];