HTML DOM - Modify HTML Content
Through the HTML DOM, JavaScript can access every element in an HTML document.
Change HTML Content
The easiest way to change an element's content is to use the innerHTML property.
The following example changes the HTML content of a <p> element:
Example
Try it Yourself »
Change HTML Style
Through the HTML DOM, you can access the style objects of HTML elements.
The following example changes the HTML style of a paragraph:
Example
Try it Yourself »
Using Events
The HTML DOM allows you to execute code when an event occurs.
When "something happens" to an HTML element, the browser generates an event:
- Clicking on an element
- Loading the page
- Changing an input field
You can learn more about events in the next chapter.
The following two examples change the background color of the <body> element when a button is clicked:
Example
Try it Yourself »
In this example, the same code is executed by a function:
Example
Try it Yourself »
The following example changes the text of a <p> element when a button is clicked:
Example
Try it Yourself »