How to get only the clicked element in JQuery
Suppose the following
<html>
<body>
<div>
<h1>headding</h1>
</div>
<a>link</a>
</body>
</html>
$("*").click(function(e){
e.preventDefault();
alert($(this)[0].tagName);
});
I need when click on h1
the alert show h1
and when click on a
the alert show a
etc.
The problem is when i click on any element the code make loop to show name of all parents element of the clicked element. but i need only the first clicked element. any help
use stopPropagation() so the event doesn't bubble up
$("*").click(function(e){
e.preventDefault();
e.stopPropagation();
alert($(this)[0].tagName);
});
FIDDLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With