masche
masche

Reputation: 1643

How to get button id at .on "click" event

Short question from a jQuery newbie:

Say I'm receiving an arry of json objects and my script generates a table row (the last td contains a button) in an existing table for each one of them. The index will be the id of the and the class is "delete".

This works so far:

$(document).on('click', ".delete", function(){
    alert('you clicked me!');
});

But I need the id of that button the send the delete request for the right object. How would I do that?

Upvotes: 0

Views: 222

Answers (3)

event.target

$(document).on('click', ".delete", function(e){
    alert('you clicked me!' + e.target.id);
});

Upvotes: 2

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28528

Here is demo

this keyword will help you.

$(document).on('click', ".delete", function(){
    alert('you clicked '+ this.id);
});

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

you can use this.id because this inside the event handler refers to the target dom element.

$(document).on('click', ".delete", function(){
    alert('you clicked me!' + this.id);
});

Upvotes: 4

Related Questions