document.write() is the first DOM manipulation function. It's only logical we start our tutorial with it, right? Using this command, you can write directly to the HTML output stream.
document.write() when the document is already loaded - otherwise, an overwrite will occur.
Theory is great, but we recommend digging deeper!
Using innerHTML to Modify Content
The easiest way of editing content in an HTML element is to use the
innerHTML property to change the content of the element
document.getElementById("p2").innerHTML = "Hello World again!";
Now, in the code example below, we change the content of the element
var element = document.getElementById("id1"); element.innerHTML = "New heading";
Altering Attribute Value
You can also change the values of HTML attributes.
Take a look at the example below. In it, we change the value of the
src attribute for the
document.getElementById("myImage").src = "background.jpg";