The HTML Dom
The HTML Dom
When a web page is loaded, the browser creates a Document Object Model of the page.
With the object model, JavaScript gets all the power it needs to create dynamic HTML:
"The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows
programs and scripts to dynamically access and update the content, structure, and style of a
document."
The W3C DOM standard is separated into 3 different parts:
In other words: The HTML DOM is a standard for how to get, change, add, or delete
HTML elements.
HTML DOM methods are actions you can perform (on HTML Elements).
HTML DOM properties are values (of HTML Elements) that you can set or change.
A property is a value that you can get or set (like changing the content of an HTML element).
Example
The following example changes the content (the innerHTML) of the <p> element
with id="demo":
Example
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
In the example above the getElementById method used id="demo" to find the element.
The innerHTML property is useful for getting or replacing the content of HTML elements.
The innerHTML property can be used to get or change any HTML element,
including <html> and <body>.
If you want to access any element in an HTML page, you always start with accessing the
document object.
Below are some examples of how you can use the document object to access and manipulate
HTML.
Method Description
Later, in HTML DOM Level 3, more objects, collections, and properties were added.
document.links Returns all <area> and <a> elements that have a href attribute 1
<!DOCTYPE html>
<html>
<body>
</form>
<p id="demo"></p>
<script>
const x = document.forms["frm1"];
document.getElementById("demo").innerHTML = text;
</script></body></html>
Example
<html>
<body>
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
</body>
</html>