How do I select only <a>
elements which doesn't have the word gallery inside its id or class attribute?
views:
77answers:
4
+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
2010-07-17 22:07:01
+1 that is a lot shorter.
Sarfraz
2010-07-17 22:12:17
You can also do this a:not([id*=gallery],[class*=gallery])
joelpittet
2010-07-17 22:13:11
@joelpittet: I guess that’s is even the correct answer since he’s looking for a disjuction and not a conjunction.
Gumbo
2010-07-17 22:14:54
@joelpittet is correct, if case anyone wants to play with it: http://jsfiddle.net/MZDgB/
Nick Craver
2010-07-17 22:15:05
@nick-craver that is a cool site, thanks for the jsfiddle.net link!
joelpittet
2010-07-17 22:22:50
+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
2010-07-17 22:07:36
That is a bit heavy handed and you should use .filter(function(){return (condition to match)}) not .each
joelpittet
2010-07-17 22:17:33
+1
A:
Use the not() method http://api.jquery.com/not/
$("a").not(document.getElementById('gallery'))
Hrishi
2010-07-17 22:09:24
+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
2010-07-17 22:18:54