Q:

How to Get the Class of the Clicked Element in jQuery

0

How to Get the Class of the Clicked Element in jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this 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>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to AJAX Submit a Form in jQuery... >>
<< How to Trigger a Click on a Link Using jQuery...