Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery selector: href attribute doesn't contain neither word1 nor word2

I'm new to JQuery, i suppose that's really simple but I can't find the right selector. I tried with this code:

$(":not(a[href=word1],a[href=word2]").click(function() {
... do something
}

but it doesn't work, in fact it seems to suck all the cpu time. Can you help me? Thanks!

like image 551
astorcas Avatar asked Sep 28 '10 13:09

astorcas


1 Answers

Here's a much more efficient version that narrows it down to anchors first:

$("a:not([href*=word1],[href*=word2])").click(function() {
  //do something
});

You can test the selector out here.

Currently you're trying to bind a click event to everything that's not an anchor with one of those words in it's href attribute...every element which is very expensive both to check and to bind, this instead binds to anchors and only then if their href doesn't contains either of those words.

like image 149
Nick Craver Avatar answered Nov 16 '22 00:11

Nick Craver