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()
{
}
USING PURE JAVASCRIPT: I know it's late but may be for the future people it can help:
In the HTML part:
In the Javascipt Controller:
This way we don't have to bind the 'id' of the Element at the time of calling the javascript function.
You can simply do it this way:
How to do it without inline JavaScript
it is generally recommended to avoid inline JavaScript, but rarely is there an example of how to do it.
Here is my way of attaching events to buttons.
I'm not entirely happy with how much longer the recommended method is compared to a simple
onClick
attribute.Modern browsers only
Cross-browser
Cross-browser with jQuery
You can run this before the document is ready, clicking the buttons will work because we attach the event to the document.
Here is a jsfiddle
For some strange reason the
insertHTML
function does not work in it even though it works in all my browsers.You can always replace
insertHTML
withdocument.write
if you don't mind it's drawbacksSources:
Button 1 Button 2 Button 3
(I think the
id
attribute needs to start with a letter. Could be wrong.)You could go for event delegation...
...but that requires you to be relatively comfortable with the wacky event model.