Link to home
Start Free TrialLog in
Avatar of Luey
LueyFlag for United States of America

asked on

change div background onChange

This code I have works to change the background color.
However I would like to be able to use the option value
to change the color of the div. Is it possible to get my option value into
my javascript?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
<!--
    function colorBox()
       {
       window.document.getElementById("little_box").style.background = "#000";
	  
}
-->
</script>
<style type="text/css">
<!--
#little_box {
	background-color: #FFF;
	height: 30px;
	width: 30px;
	border: thin solid #000000;
}
-->
</style>
</head>

<body>

<form id="form1" name="form1"  method="post" action="">
  <label onchange="javascript:colorBox();">
    <select name="color" id="color">
      <option value="0033FF">blue</option>
      <option value="FF0048">red</option>
      <option value="EDFF00">yellow</option>
    </select>
  </label>
  
</form>
<div id="little_box"></div>
</body>
</html>

Open in new window

Avatar of Terry_focus
Terry_focus
Flag of United Kingdom of Great Britain and Northern Ireland image

Yes, change to
function colorBox()
{
   window.document.getElementById("little_box").style.background = document.getElementById("color").value;
          
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Terry_focus
Terry_focus
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How about this?
document.getElementById("little_box").Style.backgroundColor = document.getElementById("form1").value;

Open in new window

Avatar of Luey

ASKER

Perfecto Thanks good job