JQuery Selector Contain Any

From CoderLearner
Jump to: navigation, search

Description

Select the attribute contains a given substring. It will select an element if the attribute value appears anywhere within the element's 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>
      <script>
         $().ready(function() {
            $('a[class*="student"]').attr('href',"student.php?j=33");
         });
      </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>

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>
         .markit {
            background-color: greenyellow;
         }
      </style>
      <script>
         $().ready(function() {
            $('a[class*="teacher"]').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