Bug Tracker

Ticket #6260 (closed bug: worksforme)

Opened 5 years ago

Last modified 4 years ago

Selector does not work properly when using multiple ":not()"s on same element

Reported by: musicisair Owned by:
Priority: undecided Milestone: 1.4.3
Component: event Version: 1.4.2
Keywords: delegate selectors not Cc:
Blocking: Blocked by:

Description

In the following HTML

<body>

<a href="#">Should alert when clicked.</a> <a href="#" class="one">Should not alert when clicked.</a> <a href="#" class="two">Should not alert when clicked.</a> <script src="js/jquery-1.4.2.js"></script> <script>

$("body").delegate("a:not(.one),a:not(.two)","click",function(){

alert("DELEGATED!");

});

</script>

</body>

$(...).delegate("a:not(.one,.two)","click",...) does work.

Change History

comment:1 Changed 4 years ago by addyosmani

  • Priority set to undecided
  • Status changed from new to closed
  • Resolution set to worksforme

In your original logic based on your HTML/JS supplied:

if a is not 'one', then it is either link 0 or link 2 a is not 'two', then it is either link 0 or link 1

on the whole this would result in the set [0,2,0,1] being assigned an alert of 'DELEGATED!' so any link clicked would fire this off. You then supplied a slightly different case (a:not(.one,.two)) which also wouldn't work.

Rather than doing this, if you are trying to just prevent links 'one' and 'two' from firing the alert whilst using delegate to still assign the click to any links, you can do this:

$("body").delegate("a:not('.one','.two')","click",function() {

alert("DELEGATED!");

});

See:  http://jsfiddle.net/dakW9/1/

$("body").delegate("a:not(':eq(1)',':eq(2)')","click",function() {

alert("DELEGATED!");

});

See:  http://jsfiddle.net/dakW9/

Both of these work as desired.

Note: See TracTickets for help on using tickets.