PDA

View Full Version : EASY - cannot document.write(b) value



bagofmilk
05-21-2012, 10:31 PM
Below is a mock snippet of what I need. I think the issue is


document.write(b)

because if I change that to an alert, it works fine. I thought I was using the proper syntax, but I guess I am not. Can anyone help a noob?




<html>
<head>
<script type="text/javascript">
function d(){
a=document.getElementById("txt1").value;

if(a > 1){
b=a/100;
document.write(b)
}
else{
b=a;
alert(b)
}
}

</script>
</head>
<body>
<form name="form">
<h2>This is a form</h2>
<input type="text" id="txt1" name="txt"/> type here <br />
<button type="submit" id="btn" onclick="d()">Submit</button>
</body>

</html>

felgall
05-21-2012, 10:39 PM
document.write can only be used BEFORE the page finishes loading.

The simplest alternative to that almost useless command to use in real web pages is innerHTML.