PDA

View Full Version : Could anyone please help me with this little script problem


b_j_mall
08-28-2002, 12:52 AM
A btton executes a fucntion that further generates a button, this button is used to execute a function, but gives an error.
The code is given below,i really need some, thanks for your time.

--------------------------------------------
<html>
<head>
<SCRIPT LANGUAGE="javascript">
function init()
{
alert('how u doin');

document.write("<form name=major>") //This generates another button
document.write("<input type=button name=start1 value=anotherOne onClick='init1()'>")//this button should //execute init1() but gives an error
document.write("</form>")
}
function init1()
{
alert('i am ok');
}
</SCRIPT>
</head>
<body onLoad='init()'>
<p>hello!</p>
<FORM NAME="joe">
<input type=button name=start value=startIt onClick='init1()'> <!-- This button click executes init() funtion-->
</FORM>
</body>
</html>

adios
08-28-2002, 02:27 AM
You're calling document.write() on a closed (fully-written) HTML document, the equivalent of loading a new page. So much for 'init1() '. Try this:

<html>
<head>
<script language="javascript">
function init()
{
alert('how u doin');
var newButton = '<input type="button" name="start1" value="anotherOne" onClick="init1()">';
document.getElementById('swapit').innerHTML = newButton;
}
function init1()
{
alert('i am ok');
}
</SCRIPT>
</head>
<body onLoad="init()">
<p>hello!</p>
<form name="joe">
<span id="swapit">
<input type="button" name="start" value="startIt" onClick="init1()">
</span>
</form>
</body>
</html>

You can also use DOM methods for element replacement.

http://www.webreference.com/js/column44/