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 reddocument.getElementById("frame").style.backgroundColor="red"; 

//Save into variablevar 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");functionchangeBg(newColor){
  frameRef.style.backgroundColor =  newColor;
}
<buttontype="button"onclick="changeBg('red')"value="red">Red</button><buttontype="button"onclick="changeBg('blue')"value="blue">Blue</button>

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