You have two errors here:
- You are selecting an element by id, but that element doesn't exists on your html.
- Your javascript have a sintax error. (Missing closing brackets on your function.
- You are using the property
sytlecolor
that doesn't exists (there's a typo on style
, and even this way, stylecolor
doesn't exists. Use style.backgroundColor
instead.
Here's a working example:
<html>
<body id="background">
<button type="button" onclick="myFunction()"> Blue</button>
<script>
function myFunction(){
document.getElementByld("background").style.backgroundColor = "blue";
}
</script>
</body>
</html>
Also if you want to change the body background color, you don't need to put an id on it:
<html>
<body>
<button type="button" onclick="myFunction()"> Blue</button>
<script>
function myFunction(){
document.body.style.backgroundColor = "blue";
}
</script>
</body>
</html>