WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

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:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties