PDA

View Full Version : Problem with creating inputs and nesting w/JS


Tails
02-15-2003, 06:35 PM
I'm trying to make an online of those "choose your own path" storybooks where you make decisions and go to the page it says.
I can do this in DOS batch files, but I'm having problems with JavaScript.
<body>
<form>
<script>
W=document.write
W('<input type="button" name="p1" value="goto Page 1" onCLick="pa1()" />')

function pa1()
{
W('<input type="button" value="goto Page 2" onClick="pa2()" />')
}

function pa2()
{
W('testing page 2')
}

</script>
</form>
</body>

The form elements don't exist after the nesting. Why? And then the error details say that the problem is at line 1 character 1. I've tried labeling names, alerting them to test, and everything. It just doesn't work. What do I do?

Algorithm
02-15-2003, 09:20 PM
The problem with document.write is that unless you use it just right, it'll overwrite the existing contents of your page. Instead, try placing a <div> tag where you want the dynamic content, and then mess with the div's innerHTML property.var divRef = null;
if(document.getElementById){
divRef = document.getElementById("divId");
} else if(document.all){
divRef = document.all["divId"];
}
if(divRef){
divRef.innerHTML = "Content here";
}

Tails
02-15-2003, 09:55 PM
Thanks, that's a big help!:)