Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

jQuery hasClass()


The jQuery hasClass() method is used to check whether selected elements have specified class name or not. It returns TRUE if the specified class is present in any of the selected elements otherwise it returns FALSE.

Syntax:

  1. $(selector).hasClass(classname)

Parameters of jQuery hasClass() method

Parameter Description
className It is a mandatory parameter. It specifies the name of the CSS class to search for in the selected elements.

Example of jQuery hasClass() method

Let’s take an example to demonstrate jQuery hasClass() method.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src=“https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js”></script>
  5. <script>
  6. $(document).ready(function(){
  7.     $(“button”).click(function(){
  8.         alert($(“p”).hasClass(“intro”));
  9.     });
  10. });
  11. </script>
  12. <style>
  13. .intro {
  14.     font-size: 150%;
  15.     color: Blue;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <h1>Look here, I am a heading.</h1>
  21. <p class=“intro”>This is a paragraph.</p>
  22. <p>This is also a paragraph.</p>
  23. <button>Click here to check if any p element have an “intro” class?</button>
  24. </body>
  25. </html>

Test it Now

jQuery hasClass() method example 2

Let’s take another example to demonstrate jQuery hasClass() method.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>The Selecter Example</title>
  5. <script type=“text/javascript” src=“http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js”>
  6. </script>
  7. <script type=“text/javascript” language=“javascript”>
  8. $(document).ready(function() {
  9. $(“#result1”).text( $(“p#pid1”).hasClass(“red”) );
  10. $(“#result2”).text( $(“p#pid2”).hasClass(“red”) );
  11. });
  12. </script>
  13. <style>
  14. .red { color:red; }
  15. .blue { color:blue; }
  16. </style>
  17. </head>
  18. <body>
  19. <p class=“red” id=“pid1”>This is first paragraph.</p>
  20. <p class=“blue” id=“pid2”>This is second paragraph.</p>
  21. <div id=“result1”></div>
  22. <div id=“result2”></div>
  23. </body>
  24. </html>

Test it Now

Here the first condition is true and the second is false. If we set the second paragraph class name ?blue? then both the conditions will be true.

jQuery hasClass() Example 3

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>The Selecter Example</title>
  5. <script type=“text/javascript” src=“http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js”>
  6. </script>
  7. <script type=“text/javascript” language=“javascript”>
  8. $(document).ready(function() {
  9. $(“#result1”).text( $(“p#pid1”).hasClass(“red”) );
  10. $(“#result2”).text( $(“p#pid2”).hasClass(“blue”) );
  11. });
  12. </script>
  13. <style>
  14. .red { color:red; }
  15. .blue { color:blue; }
  16. </style>
  17. </head>
  18. <body>
  19. <p class=“red” id=“pid1”>This is first paragraph.</p>
  20. <p class=“blue” id=“pid2”>This is second paragraph.</p>
  21. <div id=“result1”></div>
  22. <div id=“result2”></div>
  23. </body>
  24. </html>

Test it Now

Here both the conditions will be true.

jQuery hasClass() Example 4

  1. <!DOCTYPE html>
  2. <html>
  3.  <head>
  4.   <script src=“http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js”></script>
  5.   <script>
  6.   $(document).ready(function() {
  7.    $(“.btn”).click(function(){
  8.     var className = $(this).attr(“id”);
  9.     $(“ul li”).each(function() {
  10.      if ($(this).hasClass(className)) {
  11.       $(this).fadeTo(‘slow’, 0.0).fadeTo(‘slow’, 1.0);
  12.      }
  13.     });
  14.    });
  15.   });
  16.   </script>
  17.   <style>
  18.   ul{
  19.    font-family: monospace;
  20.    font-size: 15px;
  21.    font-family: monospace;
  22.    font-style: normal;
  23.    font-size-adjust: none;
  24.    width:200px;
  25.    padding:0px;
  26.   }
  27.   ul li{
  28.    background-color:#7fffd4;
  29.    width:100px;
  30.    margin:5px;
  31.    padding:5px;
  32.    list-style-type:none;
  33.    width:200px;
  34.   }
  35.   </style>
  36.  </head>
  37.  <body>
  38.  <h1>jQuery .hasClass() function Example</h1>
  39.  <ul>
  40.   <li class=“red”>Red</li>
  41.   <li class=“green”>Green</li>
  42.   <li class=“green red”>Green Red</li>
  43.   <li class=“blue”>Blue</li>
  44.  </ul>
  45.  <input type=“button” class=“btn” value=“Red Class” id=“red”>
  46.  <input type=“button” class=“btn” value=“Green Class” id=“green”>
  47.  <input type=“button” class=“btn” value=“Blue Class” id=“blue”>
  48.  <input type=“button” class=“btn” value=“No Matching Class” id=“noclass”>
  49.  </body>
  50. </html>

Comments

comments