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
Ashlynn Pai
11,679 PointsWriting from JS to the DOM
I want the user to submit a number. The number becomes a JS variable and is returned as x. Now I want to write it back to the DOM and look decent.
Right now when it writes back to the DOM it wipes the whole page out. I would like it to write back to the form or something like that.
1 Answer
James Barnett
39,199 Pointsdocument.write in this case has to open the document for writing so it calls document.open which in turn removes everything from the DOM, you don't want to do that, instead you want to modify one element.
Your jQuery to update the html of #answer is fine, once you comment out the document.write line, it will work.
Dino Paškvan
Courses Plus Student 44,108 PointsDino Paškvan
Courses Plus Student 44,108 PointsIt might be important to note that
document.writedoesn't erase the page. Ifdocument.writeis called after the page has finished loading, it implicitly callsdocument.open, which in turn opens the document for writing and empties its content.James Barnett
39,199 PointsJames Barnett
39,199 PointsDino Paškvan - I haven't done much DOM work in vanilla JS.
What's the distinction you are drawing between
overwrites everythingandopens the document for writing and empties its content. I have a feeling there might be something fundamental I missed in my cursor reading on MDN earlier.Dino Paškvan
Courses Plus Student 44,108 PointsDino Paškvan
Courses Plus Student 44,108 PointsFrom an observer's point of view, there's no difference — the end result is the same, as the content of the document gets replaced by what has been passed to
document.write. But it's notdocument.writethat does the erasing. It just makes an implicit call todocument.open.Take a look at these two examples:
As the document is already open (because it's loading),
document.writeis free to write to it, and will do so without removing anything from the DOM. Theh1will appear before theh2tag. If you move the<script>...</script>to the bottom of the body, theh1will appear after theh2, but it still won't delete the content of the document. So,document.writedoesn't overwrite anything, it adds to an open document.The next example works differently:
In this example,
document.writeis called after a 3-second delay — so it runs after the document has finished loading. Becausedocument.writecannot write to a document that's not open, it callsdocument.openwhich removes everything from the document.You can further confirm this behaviour if you navigate to some webpage, open up the JavaScript console and run
document.open()in it. You'll see that the webpage gets deleted.The distinction is that you said that
document.writeoverwrites everything when that's not true. That function cannot overwrite anything on its own. It only writes to a document which is open.James Barnett
39,199 PointsJames Barnett
39,199 Points>The distinction is that you said that document.write overwrites everything when that's not true. That function cannot overwrite anything on its own. It only writes to a document which is open.That's very instructive, I see what you mean about
document.openvsdocument.writeAshlynn Pai
11,679 PointsAshlynn Pai
11,679 PointsThanks guys, when I tried just $("#answer").html(x); last night it didn't work but I must have forgotten to save or something when I tested. Today it works! Yay!
Also Dino thanks for the explanation.