Click here to Skip to main content
15,907,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
im a noob,
change background color using radio button

What I have tried:

JavaScript
<html>
<head>
<script language="javascript">
function changeclr()
{
	var col=document.getElementById('ccolor').value;
	documnet:body.backgroundColor='col';
}
</script>
</head>
<body>
<form>
<input type="radio" name="ccolor" value="red" onclick="changeclr()"> Red<br>
<input type="radio" name="ccolor" value="blue" onclick="changeclr()"> Blue<br>
<input type="radio" name="ccolor" value="white" onclick="changeclr()"> White<br>
<form>
</body>
</html>
Posted
Updated 7-Jan-17 21:29pm
v3

Firs: You Do not declare ID="ccolor",

Second: You Can NOT declare same ID for every 3 input tags, You have to declare class="ccolor" and in your function:
JavaScript
var col = document.getElementByClassName("ccolor")[0].value;
document.body.BackgroundColor = col;
 
Share this answer
 
A small change to your function and additional input param passed.
HTML
<html>
<head>
<script language="javascript">
function changeclr(obj)
{
	document.body.style.backgroundColor=obj.value;
}
</script>
</head>
<body>
<form>
<input type="radio" name="ccolor" value="red" onclick="changeclr(this)"> Red<br>
<input type="radio" name="ccolor" value="blue" onclick="changeclr(this)"> Blue<br>
<input type="radio" name="ccolor" value="white" onclick="changeclr(this)"> White<br>
<form>
</body>
</html>
 
Share this answer
 
v3

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