Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the elements without a particular attribute by jQuery

Tags:

jquery

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:

<div id="para">     <input name="fname" optional="1">     <input name="lname">     <input name="email"> </div> 

jQuery:

$("#para [optional]") // give me the fname element   $("#para :not([optional])") //give me the fname, lname, email (fname should not appear here)   
like image 590
Billy Avatar asked Jul 02 '09 11:07

Billy


2 Answers

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 selects descendants of #para.

like image 177
Frank DeRosa Avatar answered Sep 20 '22 00:09

Frank DeRosa


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 in your code.

like image 28
Pim Jager Avatar answered Sep 18 '22 00:09

Pim Jager