With the HTML DOM, JavaScript can access every element in an
HTML document
The easiest way to change the content of an element is by using the innerHTML property.
The following example changes the HTML content of a <p> element:
With the HTML DOM you can access the style object of HTML elements.
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>
The HTML DOM allows you to execute code when an event occurs.
Events are generated by the browser when "things happen" to HTML elements:
You can read more about events in the next chapter.
The following 2 examples changes the background color of the <body> element when a button is clicked:
In this example the same code is executed by a function:
The following example changes the text of the <p> element when a button is clicked:
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
The HTML Certificate documents your knowledge of HTML.
The HTML5 Certificate documents your knowledge of advanced HTML5.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
Your message has been sent to W3Schools.