DOM (Document Object Model) – Write CSS style

Syntax:

document.getElementById(id).style.property=new style

Notice:

... property=new style ... // proprietà=nuovo stile

Example:

<!DOCTYPE html>
<html>
<body>

<p id="p1">Hello World!</p>
<p id="p2">Hello World!</p>

<script>
document.getElementById("p2").style.color="blue";       // Blu
document.getElementById("p2").style.fontFamily="Arial"; // Arial
document.getElementById("p2").style.fontSize="larger";  // Font Size
</script>

</body>
</html>

Using Events: onClick etc…

<!DOCTYPE html>
<html>
<body>

<h1 id="id1">My Heading 1</h1>
<button type="button" 
onclick="document.getElementById('id1').style.color='red'">
Click Me!</button>

</body>
</html>

My official WebSite >