Please note, this is a STATIC archive of website www.tutorialrepublic.com from 10 Sep 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES
Advertisements

How to Get the Class of the Clicked Element in jQuery

Topic: JavaScript / jQueryPrev|Next

Answer: Use the target Property

You can simply use the event.target property to get the class from any element which is clicked on the document in jQuery. In this solution there is no need to know the element beforehand.

Let's try out the following example to understand how it basically works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Class of Clicked Element</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).on("click", function(event){
    alert(event.target.className);
})
</script>
</head> 
<body>
    <button type="button" class="foo">Button 1</button>
    <input type="button" class="bar" value="Button 2">
    <button type="button" class="baz">Button 3</button>
</body>
</html>

Related FAQ

Here are some more FAQ related to this topic:

Advertisements
Bootstrap UI Design Templates