JavaScript document.getElementById Edit Background Color

We can use JavaScript like CSS to style a div element on a page to edit the background color. Lets say that you have a div id on a page called "topright" and you wanted to change the color to red with JavaScript.
<div id="topright"></div>

Use the code below to change the color to red with JavaScript.

<script>
document.getElementById("topright").style.backgroundColor = "red";
</script>

Change the color of a div to blue with JavaScript

[code lang="js"]<script> document.getElementById("topright").style.backgroundColor = "blue"; </script>

Change the color of a div to green with JavaScript

[code lang="js"]<script> document.getElementById("topright").style.backgroundColor = "green";</script>

JavaScript document.getElementById Edit Background Color

One of the best ways to learn is by tearing apart the code piece by piece to see what each character is doing. Take advantage of this code and remove parts to see what it does and then paste it back in.  That is the best way to learn JavaScript document.getElementById Edit Background Color

Sign Up To Comment