ashish2py

jQuery check if an element has a certain class

Feb 8th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery check if an element has a certain class
  2.  
  3.  
  4. <html>
  5. <head>
  6. <style type="text/css">
  7.   .redColor {
  8.     background:red;
  9.   }
  10.   .blueColor {
  11.     background:blue;
  12.   }
  13.  </style>
  14. <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
  15. </head>
  16. <body>
  17.   <h1>jQuery check if an element has a certain class</h1>
  18.  
  19.   <div class="redColor">This is a div tag with class name of "redColor"</div>
  20.  
  21.   <p>
  22.   <button id="isTest">is('.redColor')</button>
  23.   <button id="hasClassTest">hasClass('.redColor')</button>
  24.   <button id="reset">reset</button>
  25.   </p>
  26. <script type="text/javascript">
  27.  
  28.     $("#isTest").click(function () {
  29.  
  30.       if($('div').is('redColor')){
  31.         $('div').addClass('blueColor');
  32.       }
  33.  
  34.     });
  35.  
  36.     $("#hasClassTest").click(function () {
  37.  
  38.       if($('div').hasClass('redColor')){
  39.         $('div').addClass('blueColor');
  40.       }
  41.  
  42.     });
  43.  
  44.     $("#reset").click(function () {
  45.       location.reload();
  46.     });
  47.  
  48.  
  49. </script>
  50. </body>
  51. </html>
Advertisement
Add Comment
Please, Sign In to add comment