How to check if div tag has particular class or not using jQuery with Example

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.
<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 {
            backgroundpurple;
            colorWhite;
        }
 
        .blucls {
            backgroundblue;
        }
    </style>
</head>
<body>
    <form id="form1">
        <div id="divdetails" style="cursorpointerwidth20%height50pxtext-aligncenterfont-weightbold">
            Sample Div
        </div>
    </form>
</body>
</html>

Output:
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.
Previous
Next Post »

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