JQuery Selector Not Equal

From CoderLearner
Jump to: navigation, search

Description

Select elements that is not equal to a given string.

Syntax

Attribute Not Equal Selector [name!="value"]

Note

Use $("element").not('[name="value"]') for better performance.

Example 1

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Select</title>
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>
      <style>
         .mark {
            background-color: red;
         }
      </style>
      <script>
         $().ready(function() {
            $('a[class!="pass"]').addClass("mark");
         });
      </script>
   </head>
   <body> 
      <a href="#" class="pass">John</a><br/>
      <a href="#" class="failed">Christ</a><br/>
      <a href="#" class="failed">Bob</a><br/>
      <a href="#" class="pass">Weeks</a><br/>
      <a href="#" class="failed">Jobs</a><br/>
      <a href="#" class="failed">Bill</a><br/>
   </body>
</html> 

Try It

Example 2

Change URL based on the class types.

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Select</title>
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>
      <script>
         $().ready(function() {
            $('a[class!="student"]').attr("href","teacher.php");
         });
      </script>
   </head>
   <body> 
      <a href="#" class="teacher">John</a><br/>
      <a href="#" class="student">Christ</a><br/>
      <a href="#" class="student">Bob</a><br/>
      <a href="#" class="student">Weeks</a><br/>
   </body>
</html>