Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get classname using Jquery

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 437
    Comment on it

    Hello Readers,

    When we apply CSS class to HTML element sometimes we don't know that which CSS applied to HTML element.

    So, below we use jquery to get the class name.

    Here, we use the Jquery .attr() function which returns the class attribute value.

    Jquery attr('class')
    

    Syntax:

    $("#id").attr('class');
    

    Below is the code to get the class name using jquery functions and use some css to styling the content.

    <html>
        <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script>
        $(document).ready(function(){  
        $("#btn").click(function(){
        $("#demo").attr('class').split(' ').map(function(clssName){     
        $("#message").append(clssName +" ");
        alert(clssName)
        })
        });  
        });  
        </script>
        </head>
        <body>
        <h1>Get Class Name Using Jquery</h1>
        <div class="red green blue" id="demo">red green blue</div>
        <p id="message"></p>
        <p><input type="button" value="Get Class Name" id="btn"></p>
        </body>
        </html>
    

    The above code use .attr() function and split function to get the respected class names.

 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: