The btnAdd button is not directly visible in the page. You'll need to get a reference to it, using document.getElementById or by using formName.buttonName. The following code shows you how to accomplish this. It uses the onload event of the <body> tag to set the dynamic event for the button. When you click the button, method 2 will fire instead of method 1:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Dynamic Events</title>
<script language="JavaScript" type="text/javascript">
function fnDoAction()
{
alert('I am action 1');
}
function DoAction2()
{
alert('I am action 2');
}
function SetDynamicEvent()
{
document.getElementById('btnAdd').onclick = DoAction2;
// Alternatively, uncomment the next code line. Old code style
// but cross-browser.
// frmDynamicEvents.btnAdd.onclick = DoAction2;
}
</script>
</head>
<body onload="JavaScript:SetDynamicEvent();">
<form name="frmDynamicEvents">
<input type="button" onclick="JavasSript:fnDoAction()" id="btnAdd" value="Click Me">
</form>
</body>
</html>
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.