Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery get the actual control that fired the event.

I have a following html:-

<button type="button" class="btn-add">
  <i class="some-icon"></i>
</button>

which creates a nice fancy button with an icon

I bound an event on the button click:-

function (e) {
  console.log($(e.target));
}

now when i click on the center of the button the output of the above javascript is <i> element because of e.target. Is there any other way to get the actual control that fired this event, in this case the <button> ?

like image 947
anit Avatar asked Feb 02 '26 05:02

anit


1 Answers

Read this Difference between target and currentTarget

Here is your solution: Fiddle link

$('.btn-add').click(function(e) {
    alert(e.currentTarget.tagName);
});
like image 178
Sachin Avatar answered Feb 03 '26 21:02

Sachin