Hi I am new to javascript. I am having problems with my book. here is the function that works. my questions is why does this not work if I swap the line around:
document.form1["cb"+i].checked = document.form1.cb0.checked;
to:
document.form1.cb0.checked = document.form1["cb"+i].checked;
Any help would be appreciated.
Thank you
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns=" <head>
<title>Using variables</title>
</head>
<body>
<form name="form1" >
<input type="checkbox" name="cb0" value="cb0_value" onclick="selectAll()" />Check all<br />
<input type="checkbox" name="cb1" value="cb1_value" />Computers<br />
<input type="checkbox" name="cb2" value="cb2_value" />Books<br />
<input type="checkbox" name="cb3" value="cb3_value" />Pencils<br />
<input type="checkbox" name="cb4" value="cb4_value" />Paper<br />
</form>
<script language="javascript">
function selectAll() {
for(i=1; i<5;i++) {
document.form1["cb"+i].checked = document.form1.cb0.checked;
}
}
</script>
</body>
</html>
document.form1["cb"+i].checked = document.form1.cb0.checked;
to:
document.form1.cb0.checked = document.form1["cb"+i].checked;
Any help would be appreciated.
Thank you
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns=" <head>
<title>Using variables</title>
</head>
<body>
<form name="form1" >
<input type="checkbox" name="cb0" value="cb0_value" onclick="selectAll()" />Check all<br />
<input type="checkbox" name="cb1" value="cb1_value" />Computers<br />
<input type="checkbox" name="cb2" value="cb2_value" />Books<br />
<input type="checkbox" name="cb3" value="cb3_value" />Pencils<br />
<input type="checkbox" name="cb4" value="cb4_value" />Paper<br />
</form>
<script language="javascript">
function selectAll() {
for(i=1; i<5;i++) {
document.form1["cb"+i].checked = document.form1.cb0.checked;
}
}
</script>
</body>
</html>