homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
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.

5+ Year Member

Msg#: 3893181 posted 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.

<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!



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

Msg#: 3893181 posted 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.


5+ Year Member

Msg#: 3893181 posted 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