Posts: 256
Location: Auckland, New Zealand
|
Are you sure you want radio button or checkbox?
I use radio buttons for making a selection between a few choices, e.g. gender Male/Female, If you want it to act like an On, Off switch, then I'll show you that, but a checkbox serves this purpose better I think.
So here's a full working example:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-NZ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Unhide on checkboxes/radio buttons</title>
<script type="text/javascript">
function toggleLayer(val)
{
if(val == 'on' || val === true)
{
document.getElementById('b1').checked = true;
document.getElementById('a1').checked = true;
document.getElementById('layer1').style.display = 'block';
}
else if(val == 'off' || val === false)
{
document.getElementById('b1').checked = false;
document.getElementById('a2').checked = true;
document.getElementById('layer1').style.display = 'none';
}
}
</script>
</head>
<body onload="document.getElementById('layer1').style.display = 'none'; document.getElementById('a2').checked = true; document.getElementById('b1').checked = false;">
<form action="" method="post">
<fieldset>
<legend>Unhide Layer Form</legend>
<ul>
<li><label for="a1">On</label> <input id="a1" name="switcher" type="radio" value="on" checked="checked" onclick="toggleLayer(this.checked);" /> <label for="a2">Off</label> <input id="a2" name="switcher" type="radio" value="off" onclick="toggleLayer(!this.checked);" /></li>
<li><label for="b1">Check Me:</label> <input id="b1" name="b1" type="checkbox" value="on" checked="checked" onclick="toggleLayer(this.checked);" /></li>
</ul>
</fieldset>
</form>
<div id="layer1">You can now see this.</div>
</body>
</html>
Now I use Javascript to hide the layer and set the checkboxes etc, because if javascript is not enabled, then everything should default to being on and displaying and these checkboxes/radio have no effect whatsoever.
If you need more explanation with the code, then I'll be more than happy to explain it.
P.S. I've updated the script to work in IE as noted by Kirtan, I also removed questionable names because it may conflict with reserved keywords IE could have used.
Cheers,
MC
__________________
#------------------------------ signature---------------------------------------------------------------------------------#
Quote:
|
I am well recognised for what I don't do than what I do. Chores are just one of those things.
|
Last edited by mastercomputers; 09-29-2006 at 03:58 AM..
|