JQuery Selector Equal with

From CoderLearner
Jump to: navigation, search

Description

Selects elements that its attribute value matched exactly to a given string.

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>
         .mark {
            background-color: red;
         }
      </style>
      <script>
         $().ready(function() {
            $('span[class="mark-3"]').addClass("mark");
         });
      </script>
   </head>
   <body> 
      <span class="mark-3">John</span>
      <span class="mark-4">Janny</span>
      <span class="mark-5">Paul</span>
      <span class="mark-5">Christ</span>
   </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() {
            var marks_ids= ['mark-3','mark-5'];
            $.each(marks_ids, function(i, element){
               $('body').find('span[class="'+element+'"]').addClass("mark");
            })
           
         });
      </script>
   </head>
   <body> 
      <span class="mark-3">John</span>
      <span class="mark-4">Janny</span>
      <span class="mark-5">Paul</span>
      <span class="mark-5">Christ</span>
   </body>
</html>

Try It