Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is jquery hasClass() method ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 359
    Comment on it

    Hello Readers,

    hasClass() is the jquery method which is used to check the any classname of the selected elements. If we find the name of the class of the selected element it return true else return false.

    Syntax :

    $(selector).hasClass(classname)
    

    Parameter :

    classname : this is a required parameter which specifies the classname to search for in the selected elements.

    Code Example :

    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("button").click(function(){
            alert($("p").hasClass("intro"));
        });
    });
    </script>
    <style>
    .intro {
        font-size: 100%;
        color: yellow;
    }
    </style>
    </head>
    <body>
    
    <h1>This is a heading</h1>
    
    <p class="intro">This is a paragraph.</p>
    <p>This is another paragraph.</p>
    
    <button>button</button>
    
    </body>
    </html>
    

    In the above code if we find any p element have an "intro" class than hasClass() return true else return false.In the above example return true because this method find the Classname "intro" inside the p element.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: