How do find the id of the button which is being clicked?
<button id="1" onClick="reply_click()"></button>
<button id="2" onClick="reply_click()"></button>
<button id="3" onClick="reply_click()"></button>
function reply_click()
{
}
How do find the id of the button which is being clicked?
<button id="1" onClick="reply_click()"></button>
<button id="2" onClick="reply_click()"></button>
<button id="3" onClick="reply_click()"></button>
function reply_click()
{
}
You need to send the ID as the function parameters. Do it like this:
This will send the ID
this.id
asclicked_id
which you can use in your function. See it in action here.In general, things are easier to keep organized if you separate your code and your markup. Define all of your elements, and then in your JavaScript section, define the various actions that should be performed on those elements.
When an event handler is called, it's called within the context of the element that was clicked on. So, the identifier this will refer to the DOM element that you clicked on. You can then access attributes of the element through that identifier.
For example:
Sorry its a late answer but its really quick if you do this :-
This gets the ID of any button clicked.
If you want to just get value of button clicked in a certain place, just put them in container like
and change the code to :-
I hope this helps
If you don't want to pass any arguments to the onclick function, just use
event.target
to get the clicked element: