JS Tutorial
JS Version
JS Objects
JS Function
JS Classes
JS Async
JS HTML DOM
JS Browser BOM
JS Web API
JS AJAX
JS JSON
JS vs JQUERY
JS Graphics
DOM Methods
HTML DOM methods are actions performed (on HTML Elements).
HTML DOM properties are values (of HTML Elements) that are set or change.
The DOM Programming Interface
- The HTML DOM can be accessed with JavaScript (and with other programming languages).
- In the DOM, all HTML elements are specified as objects
- The programming interface are the properties and methods of each object.
- A property is a value that is used to get or set (like changing the content of an HTML element).
- A method is an action you can do (like add or deleting an HTML element).
Example
The following example changes the content (the innerHTML) of the <p> element with id=”demo”.
<!DOCTYPE html>
<html>
<body>
<h2>My First Page</h2>
<p id=”demo”></p>
<script>
document.getElementById(“demo”).innerHTML = “Hello World!”;
</script>
</body>
</html>
Output
My First Page
The getElementById Method
The most common way to access an HTML element is to use the id of the element.
In the example above the getElementById method used id=”demo” to find the element.
The innerHTML Property
The easiest way to get the content of an element is using the innerHTML property.
The innerHTML property is used for getting or replacing the content of HTML elements.
The innerHTML property get or change any HTML element, including <html> and <body>.