I have the follwing JavaScript.
Because calling document.write implicity calls document.open, which clears the document on which it has been called:
If a document exists in the target, this method clears it
After the call to document.write
, the element you're trying to get a reference to no longer exists in the DOM, and an error is thrown. If you look in the error console it should be something along the lines of the following:
TypeError: Cannot read property 'txt1' of undefined
document.write(content) writes content on the document stream.
You have to close the stream after writing on the document in order to continue the page loading
document.write("hello");
document.close();