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!

Placing checkmark next to all checkboxes on form.

Status
Not open for further replies.

LucL

Programmer
Joined
Jan 23, 2006
Messages
117
Location
US
Hi,

I've got a "Select All" button/link on my form which is suppose to place a checkmark next to each e-mail message checkbox. Then, when submitted, the backend deletes the checked e-mails.

However, I do not know how to pull (in a loop) a list of checkbox objects on a given form/document when NOT knowing their name. So, is there a way to loop and get set the checkbox.checked property to "true" for all checkboxes on a form not knowing their name/id?

Thanks!
Luc L.
 
Put all the checkboxes in a container of some sort (preferably a div) and use the getElementsByTagName on that container to grab a reference to all input elements. Then check to ensure the type is checkbox before modifying the value:
Code:
<script type="text/javascript">
function checkAll(bool) {
   var boxes = document.getElementById("checkboxContainer").getElementsByTagName("input");
   for (i = 0; i < boxes.length; i++) {
      if (boxes[i].type = "checkbox") {
         boxes[i].checked = bool;
      }
   }
}
</script>
<input type="checkbox" onclick="checkAll(this.checked)" /> Check/Uncheck all
<div id="checkboxContainer">
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
</div>

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
That worked like a charm. Thanks a lot!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top