Skip to content Skip to sidebar Skip to footer

Setting Background Color To Variable In Javascript

Ok, so I'm very new to Javascript and I was wondering how one goes about making the background color a variable. Specifically, I have three different frames, and I want two of the

Solution 1:

To manipulate the background color property with javascript you need to:

//Set to red
document.getElementById("frame").style.backgroundColor="red"; 

//Save into variable
var color = document.getElementById("frame").style.backgroundColor; 

Solution 2:

var frameRef = document.getElementById("frameId");
//alternatively  you can get all farmes in one array
//var textRef = document.querySelectorAll(".frameClass");

function changeBg(newColor){
  frameRef.style.backgroundColor =  newColor;
}
<button type="button" onclick="changeBg('red')" value="red">Red</button>
<button type="button" onclick="changeBg('blue')" value="blue">Blue</button>

Post a Comment for "Setting Background Color To Variable In Javascript"