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

22 May, 2020
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;">            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:
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="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">        </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:
CSS-Misc
HTML-Misc
JavaScript-Misc
CSS
HTML
JavaScript
JQuery
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
How to apply style to parent if it has child with CSS?
Types of CSS (Cascading Style Sheet)
How to position a div at the bottom of its container using CSS?
How to set space between the flexbox ?
How to update Node.js and NPM to next version ?
How to set the default value for an HTML <select> element ?
Hide or show elements in HTML using display property
How to set input type date in dd-mm-yyyy format using HTML ?
REST API (Introduction)