jQuery Selector [attribute!=value] by attribute not equal

Introduction

The [attribute!=value] selector selects elements who does NOT have the specified attribute value.

$("[attribute!='value']")
Parameter OptionalDescription
attribute Required. the attribute to find
value Required. the value to find

Select all <p> elements NOT containing a class attribute with the value myClassSelector:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p[class!='myClassSelector']").css("background-color", "yellow");
});//from   ww w .j  av a2  s  .c  o m
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>

<p class="myClassSelector">This is a test.</p>
<p>I am from java2s.com.</p>
<p>This is another test.</p>

Who is your favourite:
<ul id="choose">
  <li>Javascript</li>
  <li>HTML</li>
  <li>CSS</li>
</ul>

</body>
</html>



PreviousNext

Related