Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<script>
function clrchange()
{
document.bgColor=document.getElementById('s1').value;
}

function cha()
{
a=document.getElementById('s1').value;
if(a.getElementById('r1'))
{
document.getElementById('r1').style.background="red";
}
else if(a.getElementById('r2'))
{
document.getElementById('r2').style.background="green";
}
else if(a.getElementById('r3'))
{
document.getElementById('r3').style.background=a;
}
}
}
</script>
<select id=s1 onchange=clrchange()>
<option id=r1 onmouseover=cha()>red</option>
<option id=r2 onmouseover=cha()>blue</option>
<option id=r3 onmouseover=cha()>green</option>
<select>
Posted
v2

You have not defined any value attribute for the options, but trying to read the value in code.
Please provide values, else read the option text.
 
Share this answer
 
Hi. If you are trying to change the background color of the page, than your code will work fine.
But if you are trying to change the background color of the Dropdown than use below code

JavaScript
function clrchange()
{
var bgcolor = document.getElementById('s1').value;
document.getElementById('s1').style.backgroundColor = bgcolor;
}

Hope this will help you.
 
Share this answer
 
v2
Javascript Code:

JavaScript
function clrchange(SelectedColor)
{
    document.bgColor=SelectedColor;
}



HTML
<select id=s1 onchange="clrchange(this.value);">
<option id="r1" onmouseover="cha(this.id,this.value);" value="red">red</option>
<option id="r2" onmouseover="cha(this.value);" value="blue">blue</option>
<option id="r3" onmouseover="cha(this.value);" value="green">green</option>
<select>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900