In this tutorial i am going to explain how to use jQuery to check if div tag has particular class or not with good example.or how to check if div tag has particular class or not using jQuery with Example. jQuery check if element has particular class or not with good example or jQuery check if div tag contains particular class name or jQuery check if element has class attribute or not with good example. Using hasClass() method in jQuery we can check if div element has particular class or not.
To check if element has particular class or not in jQuery we need to write the code.
To check if element has particular class or not in jQuery we need to write the code.
<script type="text/javascript"> if ($(this).hasClass("demopurplcls")) $(this).removeClass("demopurplcls"); else $(this).addClass("demopurplcls"); </script>If you want complete example to check if div tag has particular class or not we need to write the code.
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>jQuery Check If Div Element has Particular Class or Not</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script> <script type="text/javascript"> $(function () { $('#divdetails').addClass("demopurplcls"); $('#divdetails').click(function () { if ($(this).hasClass("demopurplcls")) { $(this).removeClass("demopurplcls"); $(this).addClass("blucls"); } else { $(this).removeClass("blucls"); $(this).addClass("demopurplcls"); } }) }) </script> <style type="text/css"> .demopurplcls { background: purple; color: White; } .blucls { background: blue; } </style> </head> <body> <form id="form1"> <div id="divdetails" style="cursor: pointer; width: 20%; height: 50px; text-align: center; font-weight: bold"> Sample Div </div> </form> </body> </html>
Output:
I hope this tutorial will helpful to you. if you have any query please write your comment in comment box which is given below.
If you have any kind of question about any post, Feel free to ask.You can simply drop a comment below post. Your feedback and suggestions will be highly appreciated. ConversionConversion EmoticonEmoticon