JQuery Selector Contain Word

From CoderLearner
Jump to: navigation, search

Description

This selector matches each attribute values against the given string, the attribute values defined as a string delimited by white space.

Syntax

jQuery('[attribute~="value"]')

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>
         .markit {
            background-color: greenyellow;
         }
      </style>
      <script>
         $().ready(function() {
            $('a[class~="class"]').addClass("markit");
         });
      </script>
   </head>
   <body> 
      <a href="#" class="sp_03 student-class">John</a><br/>
      <a href="#" class="sp_31 student teacher">Christ</a><br/>
      <a href="#" class="sp_34 class teacher">Bob</a><br/>
      <a href="#" class="sp_35 math class">Weeks</a><br/>
   </body>
</html>

Try It

Example 2

<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() {
            $('span[class~="grade"]').addClass("mark");
         });
      </script>
   </head>
   <body> 
      <span class="mark-3 grade-pass">John</span>
      <span class="mark-4 grade-failed">Janny</span>
      <span class="mark-5 grade unknown">Paul</span>
      <span class="mark-5 grade process">Christ</span>
   </body>
</html>

Try It