Click to See Complete Forum and Search --> : Detecting if checkboxes on page and if so if checked or not...


Nanwedar
06-16-2004, 12:04 PM
Hi all. I have a page that may or may not have checkboxes on it (it's a self submitting form with many levels, so depending on where in the process the user is, they will be there or not).

I also have a function to "check all" or "uncheck all". Currently the function is hardcoded to start with all or none checked and display the "check all"/"uncheck all" accordingly.

My question: Is there an easy way to do the following?

if this page contains any checkboxes and at least 1 is checked
{
set myFlag = true
}
else
{
set myFlag = false
}

I've tried several variations to get this working but to no avail thus far. Any thoughts greatly appreciated and thanks in advance!

sciguyryan
06-16-2004, 12:18 PM
Try:


function CheckMe(){
var myFlag;
var Checks = document.getElementsByTagName("input");
for (var i = 0; i < Checks.length; i++){
if (Checks[i].type == "checkbox"){
if (Checks[i].checked){
myFlag = true;
}
}
else{
myFlag = false;
}
}
}


Note: Untested.

Nanwedar
06-16-2004, 01:17 PM
Groovy, thanks, I'm sure I can get this modified to do what I need.

As a follow up question, how could I load this function automatically on pageload without putting it my body onLoad call? Do forms have an onLoad also? Reason I ask is that I'm using a header include that holds the body tag and don't wanna fire it off on every page ).

Thanks again!