Bug Tracker

Modify

Ticket #7131 (closed bug: invalid)

Opened 3 years ago

Last modified 23 months ago

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:
Blocking: Blocked by:

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

comment:1 Changed 3 years ago by addyosmani

  • Priority changed from undecided to low
  • Status changed from new to open

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 3 years ago by snover

  • Milestone 1.4.3 deleted

Resetting milestone to future.

comment:3 Changed 2 years ago by danheberden

  • Owner set to danheberden
  • Status changed from open to assigned
  • Milestone set to 1.next

comment:4 Changed 23 months ago by john

  • Status changed from assigned to closed
  • Resolution set to invalid

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

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.