views:

77

answers:

4

How do I select only <a> elements which doesn't have the word gallery inside its id or class attribute?

+10  A: 

Try this selector:

a:not([id*=gallery], [class*=gallery])

This will select each a element that does not have “gallery” in either its id or its class atttribute value.

Or for a full ID or class name match:

a:not([id=gallery], [class~=gallery])

This will select each a element that does not have “gallery” as its ID or as a class name.

Gumbo
+1 that is a lot shorter.
Sarfraz
You can also do this a:not([id*=gallery],[class*=gallery])
joelpittet
@joelpittet: I guess that’s is even the correct answer since he’s looking for a disjuction and not a conjunction.
Gumbo
@joelpittet is correct, if case anyone wants to play with it: http://jsfiddle.net/MZDgB/
Nick Craver
@nick-craver that is a cool site, thanks for the jsfiddle.net link!
joelpittet
+1  A: 

One way is to go about like this:

$('a').each(function(){
  if ($(this).attr('class').indexOf('gallery') == -1 && $(this).attr('id').indexOf('gallery') == -1) { 
    // your code....
  }
});
Sarfraz
That is a bit heavy handed and you should use .filter(function(){return (condition to match)}) not .each
joelpittet
+1  A: 

Use the not() method http://api.jquery.com/not/

$("a").not(document.getElementById('gallery'))
Hrishi
+1  A: 

there is a hasClass selector in jquery you can use that. try this.

check for class only

$('a').each(function(){
if (!$(this).hasClass('gallery'))
  {
       //code here
  }
}); 

or check for both class and id

$('a').each(function(){
if (!$(this).hasClass('gallery') && $(this).attr('id').indexOf('gallery') == -1)
  {
       //code here
  }
});
Ghost