I have the follwing JavaScript.
<html>
<head>
<script language="JavaScript">
function fdivisible()
{
document.write("<h1> Just a javascript demo</h1>");
var x=document.forms["aaa"]["txt1"].value;
alert(x);
}
</script>
</head>
<body>
<form action="#" name="aaa">
Enter a no. : <input type="text" name="txt1" id="txt1" />
<input type="button" value="Click" onclick="fdivisible();">
</form>
</body>
</html>
The problem is, the first line of the JS function is executing and the rest are ignored. If I comment out the first line the rest of the code is executed. Can anybody explain to me why it is so?
document.writecan only be used during the initial loading of the document.If you want to insert your H1 when the function is called, you may replace
with