Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  7] [ 4]  / answers: 1 / hits: 49329  / 12 Years ago, sun, june 3, 2012, 12:00:00

Why in the code below upon the call of document.write in the function validator() the form elements (the checkbox and button) are removed from screen?



<!DOCTYPE html>
<html>
<head>
<script type=text/javascript>
function validator() {
if (document.myForm.thebox.checked)
document.write(checkBox is checked);
else
document.write(checkBox is NOT checked);
}
</script>
</head>
<body>
<form name=myForm>
<input type=checkbox name =thebox/>
<input type=button onClick=validator() name=validation value=Press me for validation/>
</form>
</body>
</html>

More From » html

 Answers
4

document.write() is used to write to the document stream.



In your case, the stream is most probably already closed when the onClick handler is called, because your document has finished loading.



Calling document.write() on a closed document stream automatically calls document.open(), which will clear the document.


[#85174] Friday, June 1, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mireyag

Total Points: 73
Total Questions: 107
Total Answers: 85

Location: Ukraine
Member since Sun, Dec 13, 2020
3 Years ago
mireyag questions
Sun, Aug 15, 21, 00:00, 3 Years ago
Wed, Dec 16, 20, 00:00, 3 Years ago
Tue, Sep 1, 20, 00:00, 4 Years ago
Sun, Jul 5, 20, 00:00, 4 Years ago
;