Possible Duplicate:
Getting the ID of the element that fired an event using JQuery
I have many buttons with ID attribute.
<button id="some_id1"></button> <button id="some_id2"></button> <button id="some_id3"></button> <button id="some_id4"></button> <button id="some_id5"></button>
Assume the user clicks on some button, and I want to alert this ID of the button the user just clicked on.
How can I do this via JavaScript or jQuery?
I want to get the ID of button user just clicked.
You can get a button ID during a click event thanks to the target property of the Event interface. The target property refers to the button element that got the click event from the user. At the same time, you can get the ID of the button from the target property via target.id .
An id on a <button> tag assigns an identifier to the button. The identifier must be unique across the page.
$("button").click(function() { alert(this.id); // or alert($(this).attr('id')); });
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