Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ID of button user just clicked? [duplicate]

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.

like image 315
Yang Avatar asked Apr 24 '12 02:04

Yang


People also ask

How do I get the click button ID?

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 .

What is a button ID?

An id on a <button> tag assigns an identifier to the button. The identifier must be unique across the page.


1 Answers

$("button").click(function() {     alert(this.id); // or alert($(this).attr('id')); }); 
like image 168
GeckoTang Avatar answered Oct 14 '22 14:10

GeckoTang