PDA

View Full Version : Controlling Checkboxes



JBottero
05-25-2009, 12:48 AM
When catcheck1 is unchecked, I want catcheck9 to uncheck. Why does this not work?

<html>
<head>
<script type="text/javascript">
// Checkbox event listeners.
function checkboxs() {
document.mapform.catcheck9.checked = false;
}
</script>
</head>
<body>
<form action="" name="mapform" id="mapform">
<input type="checkbox" id="catcheck1" name="catcheck9" onclick="checkboxs()" checked />
<input type="checkbox" id="catcheck9" name="catcheck9" checked />
</form>
</body>
</html>

Blueray2048
05-25-2009, 05:03 AM
try delete the red items ...

if you need the checkbox items checked by default.
You can write a small script and add it to window.onload eventhandler to load automatically on page load.

some recommendation:
It is best to use same value on id and name attributes.
Also, the checked should be best to rewrite as checked="checked" to be validated with (x)html.



<input type="checkbox" id="catcheck1" name="catcheck1" onclick="checkboxs()" checked />
<input type="checkbox" id="catcheck9" name="catcheck9" checked />

clueful
05-25-2009, 07:14 AM
When catcheck1 is unchecked, I want catcheck9 to uncheck. Why does this not work?

function checkboxs() {
document.mapform.catcheck9.checked = false;
}

document.mapform.catcheck9 refers to an array of checkboxes all with the name 'catcheck9'.

Your function doesn't check the state of the triggering checkbox.
This is the simplest change to do what you want, but it's a poor solution:


function checkboxs()
{
if( !document.mapform.catcheck9[0].checked )
document.mapform.catcheck9[1].checked = false;
}Are you going to want this to work with more elements?

clueful
05-25-2009, 07:18 AM
if you need the checkbox items checked by default.
You can write a small script and add it to window.onload eventhandler to load automatically on page load.
Why would you do that? It makes the form script-dependent.

some recommendation:
It is best to use same value on id and name attributes.NOT in the case of form elements which are to be grouped by name.