JavaScript - HTML DOM Methods
HTML DOM Methods and Properties
In the HTML DOM, methods are actions that can be performed on HTML elements, while properties are values that can be set or changed.
The DOM Programming Interface
The HTML DOM can be manipulated using JavaScript or other programming languages. In the DOM, all HTML elements are represented as objects, and the interface to these objects is through their properties and methods.
- Property: A value you can get or set, such as changing the content of an HTML element.
- Method: An action you can perform, such as adding 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>
<p id="demo"></p>
<script>document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
In this example, getElementById is a method, and innerHTML is a property.
The getElementById Method
The getElementById method is the most common way to access an HTML element by its unique id.
In the example above, the getElementById method is used to find the element with the id "demo".
The innerHTML Property
The innerHTML property is the simplest way to get or change the content of an HTML element.
The innerHTML property is useful for retrieving or replacing the content of any HTML element, including <html> and <body>.