Isdj
Isdj

Reputation: 1856

How to select element has attribute and attribute not equal to specific value using jquery?

I got the following line:

$("element").find("[attr!='val'][attr!='val'][attr!='val'][attr!='val'][attr!='val']") 

based on this answer. My page stops running. My assumption is that it finds all the different elements in the page, also the ones that don't have the attribute. Is that correct? If so how can I fix this? I tried the

$("element").not("[attr='val']") 
// instead of 
$("element").find("[attr!='val']")

and it still crashed the page. The following code works of course:

$("element").find("[attr='val']")

Upvotes: 1

Views: 2125

Answers (1)

Mohammad
Mohammad

Reputation: 21489

JQuery Has Attribute Selector [name] select element only has attribute without consider to it value. You need to adding [attr] at the first of your selector.

$("ul").find("[class][class!='A']").css("color", "red");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li class="A">A</li>
  <li class="A">A</li>
  <li class="B">B</li>
  <li>C</li>
</ul>

Upvotes: 3

Related Questions