In this page there\'s a way to dynamic add html (textbox,input button and radio elements with javascript
my questioon is how can i add an event to the button, imagi
Probably the easiest cross-browser way is to set the event name as a property of the element:
Element.onclick = function ()
{
// do something...
}
Element.onmouseup = function ()
{
// do something else...
}
Simply, use addEventListener
method:
buttonRef.addEventListener("click", function() {
alert("Blah blah...");
}, false);
(You'll have to Google for cross-browser solution. IE doesn't support addEventListener
)
Where buttonRef
is a reference to button. How can you get that reference? There's lots of ways to do that. You can use document.getElementById()
or any other method from this "family".
var element = document.createElement("input");
element.onclick = function() {alert('Clicked!');};
You have to attach the new event when creating the DOM element.
For example :
var e = document.createElement('input');
e.onclick = function()
{
alert('Test');
};