homepage Welcome to WebmasterWorld Guest from 54.197.19.35
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Button Question
Output text is replacing my button rather that appearing below it.
1337Mac




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

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.

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

Thanks in advance for any help!

 

Fotiman




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

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.

1337Mac




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved