Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Button Question

Output text is replacing my button rather that appearing below it.



7:42 pm on Apr 15, 2009 (gmt 0)

5+ Year Member

Hi. Why is it that when this button is clicked the text replaces the button rather than appears below it?
Also, the browser continues to think, as if it is looping somehow. Not sure what's going on.

<input name="button" type="button" value="Click Me" onclick="clicked();"/>
<br />
<script type="text/javascript">
function clicked() {
document.write("You clicked the button.");

Thanks in advance for any help!


8:43 pm on Apr 15, 2009 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

You are calling document.write after the page has loaded. When you do that, whatever you write will REPLACE the document. This is one reason why document.write should be avoided. And since it replaced your document, the browser is probably waiting for <html></html> so it knows to stop processing.

Instead of document.write, use DOM methods like appendChild and createNode or createTextNode.


9:19 pm on Apr 15, 2009 (gmt 0)

5+ Year Member

OK. Thanks Fotiman.

I haven't learned of those methods yet. Looks like I've got more studying to do...
All of the tutorials I've been following so far use document.write so I just figured it was the thing to do.


Featured Threads

Hot Threads This Week

Hot Threads This Month