tags:

views:

238

answers:

3

I know how to get elements with a particular attribute:

$("#para [attr_all]")

But how can I get the elements WITHOUT a particular attribute? I try

 $("#para :not([attr_all])")

But it doesn't work. What is the correct way to do this?

Let me give an example.

$("#para [optional]") <--- give me the fname element
$("#para :not([optional])") <--- give me the fname, lname, email (fname should not appear here)

+3  A: 

Try $("#para [attr!=val]").

Your :not really should work, though, which makes me suspect something else is going on.

Edit (Machine): For a full list of attribute selectors, see this link to the JQuery Selectors docs

chaos
It doesn't work.
Billy
Yeah, I think your problem is in the context, not this selector.
chaos
+1. Do note that this selector is JQuery-specific. It's not defined as a CSS attribute selector, so it can not be used to target elements for styling (via CSS): http://www.w3.org/TR/CSS2/selector.html#attribute-selectors
PatrikAkerstrand
+2  A: 

First thing that comes to my mind (maybe sub optimal)

    $('p').filter(function(){
         return !$(this).attr('attr_all');
    });

However p:not([attr_all]) should work so I think something else is going on.

Pim Jager
I finally solved the problem and it was caused by another part of code and so I will mark this as the answer.
Billy
+2  A: 

If your code example is the exact code you're using, I think the problem is an errant space.

$("#para :not([attr_all])")

should be

$("#para:not([attr_all])")

If you leave a space in there, it changes the meaning of the selector.

Frank DeRosa
this is the right answer
Dan