In this example you will learn, how to Get the Class of the Clicked Element in jQuery. 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!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-1.12.4.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> |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.