Skip to content Skip to sidebar Skip to footer

Javascript, Get The Name Of A Clicked Button

Trying to get the id or name of the button that has been clicked. But I only get an undefined message in the popup when I try to access the id or name of the button. Could someone

Solution 1:

Basically, try

alert(this.id);

instead of

alert(button.id);

But this question shows a minimum research effort, because it has been answered multiple times here, and a simple search would suffice:

How to get ID of button user just clicked?

Getting the ID of the element that fired an event

Get id of element on button click using jquery

how to get the id when a certain button is clicked using jquery

Solution 2:

In pure javascript, you would do something like this

$("button.std_buttons").click(function (event) {
    var button = event.target;
    alert(button.id);
});

read more about event object here https://developer.mozilla.org/en/docs/Web/API/Event

Solution 3:

You need to use attr() to get the id of your button because button is a jQuery object:

alert(button.attr('id');

or you can keep your selector but do not convert it to jQuery object:

var button = this;
alert(button.id);

Solution 4:

you just use attr(), the properties are accessed using the attr("property name") in jquery

button.attr('id');

or just

 $(this).id

Post a Comment for "Javascript, Get The Name Of A Clicked Button"