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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking checboxes

Status
Not open for further replies.

TheConeHead

Programmer
Aug 14, 2002
2,106
US
I have a set of checkboxes all named the same - how can I make a link "Select all" to have them all checked?

[conehead]
 
Try the following in a function:

Code:
var cboxes = document.getElementsByName('checkboxname');
for (var ci=0;ci<cboxes.length;ci++)
  {
  cboxes[ci].checked = true;
  }
[code]

Lee
 
Code:
<script type="text/javascript">

function checkAll() {
   var checkBoxes = document.forms["blahForm"].elements["chk"];
   for (i = 0; i < checkBoxes.length; i++) {
      document.forms["blahForm"].elements["chk"][i].checked = true;
   }
}

</script>

<a href="#" onclick="checkAll();return false;">Check All</a><br>

<form name="blahForm">
   <input type="checkbox" name="chk" />
   <input type="checkbox" name="chk" />
   <input type="checkbox" name="chk" />
   <input type="checkbox" name="chk" />
   <input type="checkbox" name="chk" />
   <input type="checkbox" name="chk" />
   <input type="checkbox" name="chk" />
   <input type="checkbox" name="chk" />
</form>


-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
duh.... of course it would be smart to use my checkBoxes variable reference to the collection instead of using the element collection when setting the checked status.....

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Two ways of accomplishing the exact same thing. Must be 2fer1 day. :)#

Lee
 
yeah, but getElementsByName is nicer, less typing [smile]

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
It was also less typing when I missed the slash in to close the code section. :)#

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top