Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select all checkbox function

Status
Not open for further replies.

kzn

MIS
Jan 28, 2005
209
GB
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>
 
Hey,

what is the goal of the modification: document.form1.cb0.checked = document.form1["cb"+i].checked;
?

What do you want achieve?

If you use the line I mentioned above the script loops through all available checkboxes and sets the 'checked' status of each checkbox to the first one.

So if you check the first checkbox the script is executed and results in that the first checkbox will get the same checked status as the last checkbox.

So if the last checkbox is checked, also the first checkbox will be checked if you click on it. If the last box isn't checked you won't be able to check the first one.

So, I'm not sure if I understood your question correctly. ;-)

Cheers
Mathias
 
Like preachie, I am not sure what you're asking. Check this out

Code:
[URL unfurl="true"]http://www.w3schools.com/js/tryit.asp?filename=tryjs_form_checkbox[/URL]

This link should give you a pretty good idea on how this works.

--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top