View Full Version : How to change background color using JavaScript

12-07-2011, 08:54 PM
I want to be able to put a bar of a certain color across the top of a page. I can do this by putting two newlines in a header (h1) and using style="background-color:#xxxxxx;". Is there a way I can set this color using a javascript variable that is set using a value in a cookie? I am able to get the value from the cookie, but can't set the header background color using the variable.

12-08-2011, 12:03 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<script type="text/javascript">

function Select(id,value,days){
if (value){
document.cookie=id+'='+value+';expires='+(new Date(new Date().getTime()+days*86400000).toGMTString())+';path=/';

function cookie(nme){
var re=new RegExp(nme+'=[^;]+','i');
if (document.cookie.match(re)){
return document.getElementById(nme).style.backgroundColor=document.cookie.match(re)[0].split("=")[1];



<body onload="cookie('tst');">
<h1 id="tst" style="background-Color:red;" >
Some Text

<select name="tom" size="1" onchange="Select('tst',this.value,1)">
<option value="">Select Color</option>
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>