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 trialFawad Shah
32 Pointswhat difference between document.write() and document.innerHTML
hi im begginer to java script im confuse to understand document.write() and document.innerHTML
2 Answers
Austin Flagler
10,227 PointsI'm not an expert, but I just took this course and this is what I understood from what I watched.
document.write() puts the contents directly to the browser where the user can see it.
document.innerHTML is changing the HTML of the object you apply it to.
Essentially, you could use document.write() without any connected HTML, but if you already have HTML that you want to change, then document.innerHTML would be the obvious choice.
Hopefully I can shed at least a little bit of light on the question.
Ari Misha
19,323 PointsHiya Fawad! "Document" is a global object and a representational of your web page. So yeah whenever you chain a method to "document" object, it means we are pretty much interacting with HTML and CSS, and whatever else represents a web page. We are modifying and over writing functionalities of the current web page.
"Document" object has a lots of methods including "Document.write()" and "Document.innerHTML". "Document.write()" writes anything you assign it to, on the "Document" aka the current web page right? So yeah thats what "Document.write()" does, as it name suggests.
"Document.innerHTML" helps ya revealsthe inner HTML of an element , exactly like its name suggests. But the important thing to notice here is its outputs a string , representing the HTML contents of an element. Here is an example of "Document.innerHTML":
//getting the HTML content and changing it
document.getElementById("myP").innerHTML = "Hello Dolly.";
document.getElementById("myDIV").innerHTML = "How are you?";
I hope it helped. (:
Fawad Shah
32 PointsFawad Shah
32 Pointsthank you