Sunday, September 22, 2024
Google search engine
HomeLanguagesJavascriptHow to get the class of a element which has fired an...

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:

  • In this article we only use click event, However the approach can be applied to any event
  • onCLick() event is used in this approach.
  • This this.getAttribute(‘class’) method returns the class of the element on which event occurred. When user clicks on any element then this click event fires and we detect the class of element.
  • Use onCLick = function(this.getAttribute(‘class’)) to get the class name of the particular element.

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;">
            neveropen
        </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:

  • onCLick() event is used in this approach.
  • This $(this).attr(‘class’) method returns the class of the element on which event occurred. Whichever element has fired the event we can detect its class.
  • Use onCLick = function($(this).attr(‘class’)) to get the class name of the element which has fired the event.

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;">
            neveropen
        </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:

RELATED ARTICLES

Most Popular

Recent Comments