May 09, 2021 HTML DOM
With HTML DOM, JavaScript has access to every element in an HTML document.
The easiest way to change the content of an element is to use the innerHTML property.
The following example changes the HTML content of the element:
HTML DOM allows you to access style objects for HTML objects.
The following example changes the HTML style of a paragraph:
<p id="p2">Hello world!</p>
<script>
document.getElementById("p2").style.color="blue";
</script>
</body>
</html>
HTML DOM allows you to execute code when an event occurs.
When an HTML element "something happens," the browser generates an event:
You can learn more about events in the next chapter.
The following two examples change the background color of the element when the button is clicked:
In this case, the same code is executed by the function:
The following example changes the text of the element when the button is clicked:
These 5 examples can help you better understand how to modify HTML content, and we recommend that you take a closer look and get started before you learn about the HTML DOM elements in the next chapter