Open In App

How to get the class of a element which has fired an event using JavaScript/JQuery?

Given an HTML document and some event is fired, the task is to get the class of the element that has fired the event. Here are 2 approaches discussed.

Approach 1:



Example:This example implements the above approach.




<!DOCTYPE html>
<html>
    <head>
        <title>
            Getting the class of the element that
            fired an event using JavaScript.
        </title>
        <style>
            #div {
                background: green;
                height: 100px;
                width: 200px;
                margin: 0 auto;
                color: white;
            }
              
            #gfg {
                color: green; 
                font-size: 20px; 
                font-weight: bold;
            }
        </style>
    </head>
    <body style="text-align: center;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
        <p>
            Click on the DIV box or button to get Class of 
            the element that fired click event.
        </p>
        <div class="DIV" id="div" 
             onClick="GFG_click(this.getAttribute('class'));">
            This is Div box.
        </div>
        <br />
        <button class="button" id="button"
                onClick="GFG_click(this.getAttribute('class'));">
            Button
        </button>
        <p id="gfg"></p>
        <script>
            var el_down = document.getElementById("gfg");
            function GFG_click(className) {
                // This function is called, when the 
                // click event occurs on any element.
                // get the classname of element.
                el_down.innerHTML = "Class = " + className; 
            }
        </script>
    </body>
</html>

Output:

Approach 2:



Example: This example implements the above approach.




<!DOCTYPE html>
<html>
    <head>
        <title>
            Getting the class of the element that
            fired an event using JavaScript.
        </title>
        <style>
            #div {
                background: green;
                height: 100px;
                width: 200px;
                margin: 0 auto;
                color: white;
            }
            #gfg {
                color: green;
                font-size: 20px;
                font-weight: bold;
            }
        </style>
        <script src=
        </script>
    </head>
    <body style="text-align: center;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
        <p>
            Click on the DIV box or button to get 
            Class of the element that fired click event.
        </p>
        <div class="DIV" id="div" 
             onClick="GFG_click($(this).attr('class'));">
            This is Div box.
        </div>
        <br />
        <button class="button" id="button"
                onClick="GFG_click($(this).attr('class'));">
            Button
        </button>
        <p id="gfg"></p>
        <script>
            var el_down = document.getElementById("gfg");
            function GFG_click(className) {
                // This function is called, when the 
                // Click event occurs on any element.
                // Get the class Name.
                el_down.innerHTML = "Class = " + className; 
            }
        </script>
    </body>
</html>

Output:


Article Tags :