The Wayback Machine - https://web.archive.org/web/20240619174600/https://www.geeksforgeeks.org/how-to-find-the-class-of-clicked-element-using-jquery/
Open In App

How to find the class of clicked element using jQuery ?

Last Updated : 18 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 element to the desired class.

Approach:

Here, we use this.className property to get the class name of the current div element. Here, we have added five div elements with different class name and then use jQuery this.className property to get the clicked div elements class name and display it on the console.

Syntax:

$("div").click(function () {
    var getClass = this.className;
    console.log(getClass);
});

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>
        How to find the class of the 
        clicked element using jquery?
    </title>
  
    <!-- Import jQuery cdn library -->
    <script src=
    </script>
  
    <script>
        $(document).ready(function () {
            $("div").click(function () {
                var getClass = this.className;
                console.log(getClass);
            });
        });
    </script>
</head>
  
<body style="text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <h3>
        How to find the class of the 
        clicked element using jquery?
    </h3>
  
    <div class="main">Main Div</div>
    <div class="GFG">GFG</div>
    <div class="Geeks">Geeks</div>
    <div class="G4G">GeeksforGeeks</div>
    <div class="welcome">Welcome</div>
</body>
  
</html>


Output:



Similar Reads

Toggle class by adding the class name when element is clicked and remove when clicked outside
In this article, the task is to toggle a CSS class when clicked on that element and remove that particular class when clicked outside that element, that is, anywhere else in the HTML document. Approach: This can be easily accomplished with the help of JavaScript by following the steps below: We will first select the element on which the class has t
2 min read
How to display the tag name of the clicked element using jQuery ?
Given a HTML document containing some elements and the task is to display the clicked element. Approach: First we create a HTML document containing some elements and add a jQuery function to display the clicked element. We select the HTML body using jQuery selector and when user click the element then get the element name and display the element na
1 min read
How to get the background color of a clicked division using jQuery ?
In this article, we will learn how to get the background color of a clicked division using jQuery. Approach: All the divisions on the page are first selected using a common selector and a click binding is applied to trigger the color detection using the click() method. The division that is then currently clicked can then be found out by using this
2 min read
How to get styles of a clicked division using jQuery ?
In this article, we will learn how to get the styles (width, height, text color, and background color) of a clicked division using jQuery. Approach: The css() method is used to get the CSS property values of the selected element in jQuery. We use the css() method in jQuery that takes an array of names of properties and return an array of values of
2 min read
How to increase the width of a div by specified pixels once it is clicked using jQuery ?
In this article, we will learn how to increase the width of a division by specified pixels once it is clicked using jQuery. We can do this task using the width() method that is used to return and set the width of an element in JQuery. So when we click on it a function is called in which first we store the current width of the div in a variable usin
2 min read
How to get the ID of the clicked button using JavaScript/jQuery ?
Sometimes a developer has to know the ID of the button that the user has clicked to decide further course of action on a webpage. So, to get the ID of the clicked button using JavaScript/jQuery we have different approaches which are as follows: Table of Content Using JavaScript onClick eventUsing jQuery on() MethodUsing jQuery click() MethodApproac
4 min read
How to hide all heading elements on a page when they are clicked in jQuery ?
In this article, we will see how to hide all heading elements on a page when they are clicked in jQuery.Here we have two common approaches. Using slideUp() methodUsing hide() method Approach 1: Using slideUp() method To hide all heading elements from the page, we use slideUp() method. First, we use the click() method to detect the element is clicke
2 min read
How to check if the clicked element is a div or not in JavaScript ?
Given an HTML document, which contains many elements. The task is to determine whether the clicked element is DIV or not with the help of JavaScript. Two approaches are discussed here, first approach uses the tagName property of element and second uses instanceOf operator to search for DIV. Approach 1: Use element.tagName property, which returns th
2 min read
How to show a span element for each rows clicked in AngularJS ?
In this article, we will see how to display a span element for each row clicked in AngularJS. A span element is used to group in-line elements in a document. It can be used to make a hook to a particular part of the document which may be subject to a particular action based on DOM events. The span element can be used to highlight, show, hide or do
4 min read
How to select all text in HTML text input when clicked using JavaScript?
This example shows how to select entire input in a single click using javaScript. Syntax: &lt;input onClick="this.select();" &gt; or document.getElementById("ID").select(); Example-1: Using "this.select()". &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Select all text in HTML text input when clicked &lt;/title&gt; &lt;/head&gt; &lt;
1 min read