Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
How to get the ID of the clicked button using JavaScript/jQuery ?
Next article icon

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

Last Updated : 20 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

One can use JavaScript and JQuery to retrieve the class of an element that triggers an event. By capturing the event and accessing the class attribute, developers can dynamically identify the specific element involved in the interaction.

Below are the methods to get the class of an element that has fired an event:

Table of Content

  • Using JavaScript
  • Using JQuery

Method 1: Using JavaScript

In this approach, we are using the JavaScript onClick() event. The this.getAttribute('class') method enables the retrieval of the class of the element that triggered the event. When a user interacts with any element, the click event is triggered, allowing us to capture the class of the respective element. To obtain the class name of the specific element, utilize the syntax `onClick = function(this.getAttribute('class'))`.

Example: To demonstrate getting the class of the element that has fired an event using JavaScript.

HTML
<!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>         let 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:

GettingClass
Output

Using JQuery

In this approach, the onClick() event is utilized, triggering when a user interacts with an element. The $(this).attr('class') method retrieves the class of the element on which the event occurred, enabling the identification of the triggering element's class. To retrieve the class of the element that fired the event, the syntax onClick = function($(this).attr('class')) is used.

Example: To demonstrate getting the class of the element that has fired an event using JQuery.

HTML
<!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;">     <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>         let 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:

GettingClass
Output

Next Article
How to get the ID of the clicked button using JavaScript/jQuery ?

P

PranchalKatiyar
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JQuery
  • JavaScript-Misc
  • JavaScript-Questions
  • jQuery-Questions

Similar Reads

  • How to remove a Class name from an Element using JavaScript ?
    Removing a class name from an HTML element using JavaScript is often used to dynamically modify the styling or behavior of an element based on user interactions, events, or other conditions. The operation can be achieved using classList property, that allows you to add, remove, or toggle classes. Sy
    3 min read
  • How to get the ID of the clicked button using JavaScript/jQuery ?
    To get the ID of a clicked button using JavaScript/jQuery means identifying which specific button was clicked within a webpage. This is useful when multiple buttons exist, and you need to distinguish between them based on their unique ID for further processing. So, to get the ID of the clicked butto
    3 min read
  • How to get the child element of a parent using JavaScript ?
    Given an HTML document, the task is to select a particular element and get all the child elements of the parent element with the help of JavaScript. Below are the approaches to get the child element of a parent using JavaScript: Table of Content By using the children propertyBy using the querySelect
    3 min read
  • How to attach an event to an element which should be executed only once in JavaScript ?
    In this article, we will learn how to attach an event to an element which should be executed only once. There are two approaches that can be used here: Approach 1: In this approach, we will be using the jQuery one() method. This method attaches an event to the element with the specified selector. It
    3 min read
  • How to Access <tr> element from Table using JavaScript ?
    Given an HTML table and the task is to access the table element from the Controller and highlight any row that we want. Approach: We will use a basic DOM operation in JavaScript to access table row element. We will be adding highlight class to the row that we click, if the highlight class is already
    2 min read
  • How to Select All Elements without a Given Class using jQuery?
    In jQuery, selecting elements that do not have a specific class can be useful when you want to manipulate or style all elements except those matching a certain condition. This approach allows for more precise control over the elements in various dynamic situations. Here we have some common approache
    2 min read
  • How to find the class of clicked element using jQuery ?
    In this article, we will find the class of the clicked element using jQuery. To find the class of clicked element, we use this.className property. The className property is used to set or return the value of an element’s class attribute. Using this property, the user can change the class of an eleme
    1 min read
  • How to Test if an Event Handler is Bound to an Element in jQuery (JavaScript)?
    To test if an event handler is bound to an element in jQuery, you can use a few simple methods. These help you check if an event listener is attached to an element, which is useful for debugging. Using .data() to Check Event HandlersYou can use the .data() method to check if any event handlers are a
    2 min read
  • How to get the type of DOM element using JavaScript ?
    The task is to get the type of DOM element by having its object reference. Here we are going to use JavaScript to solve the problem. Approach 1: First take the reference of the DOM object to a variable(Here, In this example an array is made of IDs of the element, then select random ID and select tha
    2 min read
  • How to Filter a DIV Element Based on its Class Name using JavaScript?
    Div Element can be filtered based on class name for displaying specific content using JavaScript. Here, we will explore two different approaches to filtering a DIV element. Table of Content Using querySelectorAll and classListUsing getElementsByClassNameUsing querySelectorAll and classListIn this ap
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences