JS HTML DOMLesson #157
HTML DOM - Changing CSS
The HTML DOM allows JavaScript to change the style of HTML elements.
Changing HTML Style
The HTML DOM allows JavaScript to change the style of HTML elements.
To change the style of an HTML element, use this syntax:
document.getElementById(id).style.property= new style
Example
Example Code
htmlHello World!
document.getElementById("p2").style.color = "blue";Using Events
The HTML DOM allows you to execute code when an event occurs.
Events are generated by the browser when "things happen" to HTML elements:
- An element is clicked on
- The page has loaded
- Input fields are changed
You will learn more about events in the next chapter of this tutorial.
Example
Example Code
html# My Heading 1
Click Me!More Examples
Example
Example Code
htmlClick Me!HTML DOM Style Object Reference
For all HTML DOM style properties, look at our complete HTML DOM Style Object Reference.
Interactive Code Examples
JS Example 1
htmlInteractive runnable JavaScript example from EA Academy.
Live Output Preview:
JS Example 2
htmlInteractive runnable JavaScript example from EA Academy.
Live Output Preview:
JS Example 3
htmlInteractive runnable JavaScript example from EA Academy.
Live Output Preview: