Get clicked element using jQuery on event?
Asked Answered
S

6

123

I'm using the following code to detect when a dynamically generated button is clicked.

$(document).on("click",".appDetails", function () {
    alert("test");
});

Normally, if you just did $('.appDetails').click() you could use $(this) to get the element that was clicked on. How would I accomplish this with the above code?

For instance:

$(document).on("click",".appDetails", function () {
    var clickedBtnID = ??????
    alert('you clicked on button #' + clickedBtnID);
});
Sibel answered 18/4, 2013 at 19:49 Comment(0)
K
166

As simple as it can be

Use $(this) here too

$(document).on("click",".appDetails", function () {
   var clickedBtnID = $(this).attr('id'); // or var clickedBtnID = this.id
   alert('you clicked on button #' + clickedBtnID);
});
Komarek answered 18/4, 2013 at 19:50 Comment(6)
Looks like I'm dealing with a caching issue from the server. Sorry.Sibel
A warning to those using ES6. Arrow functions do not set 'this' so this will be the parent. So, basically don't use an arrow function here.Gunther
@Gunther I say you turn that into an answerBrittne
@Gunther Saved me more dozens of minutes of frustration, thank you.Subtend
@Gunther you just saved me! i was trying to figure out why it wasn't woking for like 30 minutes... Thank you!Melanson
@Gunther Funny how trivial things can make you almost smash your PC. Saved me a couple of minutes.Scrabble
B
70

You are missing the event parameter on your function.

$(document).on("click",".appDetails", function (event) {
    alert(event.target.id);
});
Blackout answered 18/4, 2013 at 19:53 Comment(1)
This was perfect for me. It gives the element that was clicked - $(this) only gave the element that had the event attached.Davidadavidde
M
52

The conventional way of handling this doesn't play well with ES6. You can do this instead:

$('.delete').on('click', event => {
  const clickedElement = $(event.target);

  this.delete(clickedElement.data('id'));
});

Note that the event target will be the clicked element, which may not be the element you want (it could be a child that received the event). To get the actual element:

$('.delete').on('click', event => {
  const clickedElement = $(event.target);
  const targetElement = clickedElement.closest('.delete');

  this.delete(targetElement.data('id'));
});
Mcginnis answered 25/10, 2017 at 5:58 Comment(1)
You should use event.currentTarget to get the element the event is bound to. event.target is indeed the element that triggered the event. Your code clickedElement.closest('.delete'); could fail if you have multiple nested elements with this class.Statist
I
2

There are many ways you can do that

The first method is by using the javascript target

$(document).on("click",".appDetails", function (event) {
    var clickebtn = target.event.id;
});
Illfounded answered 14/7, 2021 at 12:31 Comment(1)
event.target.id !Precontract
M
1
$(".masonry__img").click((e) => {
      console.log(e.currentTarget.currentSrc);
});

This will add an onClick handler to each image with the class masonry__img.

Mapes answered 14/4, 2022 at 16:6 Comment(0)
I
0

A simple way is to pass the data attribute to your HTML tag.

Example:

<div data-id='tagid' class="clickElem"></div>

<script>
$(document).on("click",".appDetails", function () {
   var clickedBtnID = $(this).attr('data');
   alert('you clicked on button #' + clickedBtnID);
});
</script>
Illfounded answered 9/5, 2020 at 19:49 Comment(1)
What is .appDetails here?Swanskin

© 2022 - 2024 — McMap. All rights reserved.