Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables The Variable Challenge Solution

Dawn Mercurio
Dawn Mercurio
2,501 Points

Document.write - Can I get it to clear the page?

I tried to use the document.write to preview each part of the sentence with the new variables in it before revealing the whole sentence. I was surprised to discover that it would just add my next document.write statement after the previous one instead of rewriting it.

After some quick research, it looks like this might not be simple, but I thought I'd ask here.

TIA.

1 Answer

Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey Dawn,

I hope that I understood your question correctly. If so, you can simply use the innerHTML property of the body and set it to an empty string. The body will be empty then and when you add your next document.write there should only be the output of this one on the page.

Like in this example:

document.write("Hello!");
document.body.innerHTML = "";
document.write("Hello again!");

In this case you would only see "Hello again!" on the page. I hope that helps, if not, feel free to ask further questions! :)