21

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>

6 Answers 6

65

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.

1
  • 2
    @JeromeWAGNER—No. Read the specification linked from the page you referenced: "Open a document stream for writing. If a document exists in the target, this method clears it."
    – RobG
    Jun 13, 2015 at 6:55
15

A document.write() called after the page loads will overwrite the current document.

You want to set the text of some element within your document. If you add a

<p id="message"></p>

to the end of your body, then you can call

document.getElementById("message").innerHTML = "your text here";

Or with the jQuery library

$("#message").html("your text here");
2
  • 1
    It should be $("body").append("your text here"); Jun 3, 2012 at 21:13
  • changed some things around - setting the html of a <p> with an id instead of just appending to the body - a bit cleaner this way
    – Andy Jones
    Jun 3, 2012 at 21:18
12

Calling document.write after the document has been loaded implicitly calls document.open, which clears the current document. (Compare to calling document.open while the page is loading, which inserts the string into the document stream; it does not clear the document.)

document.write is one of the oldest vestiges of ancient JavaScript, and should generally be avoided. Instead, you probably want to use DOM manipluation methods to update the document.

1
  • This was exactly what I was looking for: document.open(); Thanks!
    – Harlin
    Mar 17, 2019 at 13:28
3

you can use

alert("Checkbox is checked");

or if you will be displaying it on page, first create an element with an id "message" (you can write anything you want, but remember, id's have to be unique on the page) like

<div id="message"></div>

and then use

document.getElementById("message").innerHTML = "Checkbox is checked";

or if you are using jQuery:

$("#message").html("Checkbox is checked");

or if you are using a console enabled browser (Firefox, Chrome etc.)

console.log("Checkbox is checked");

instead of

document.write("Checkbox is checked");
1
        var x = document.createElement("div")
        document.body.appendChild(x)
2
  • I Fixed It! Fixed Document.write! Oct 15, 2016 at 14:00
  • I removed the </div> and only grabbed the lines from var x ... to x.innerHTML ..., and I found this worked in my application. (I had to add semicolons, too.)
    – durette
    Sep 13, 2018 at 18:12
0

document.write is not best way to go as if there is any, yes ANY, DOM manipulation happening or happened already then document.write() does not work very well as it modifies original document but ignores any changes to DOM tree.

Also remember that browser displays things primarily from DOM, not from original document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.