Click to See Complete Forum and Search --> : Retrieving all checkboxes:urgent


swatid18
01-13-2004, 11:46 PM
Hi all,
I need to retrieve all the checkboxes(to check their state). I would like to know if there is some optimized way where by i can do so without having to parse through all the elements in the form(there are lot of hidden fields on the page which i want to avoid to parse).
regards,
Swati

fredmv
01-13-2004, 11:49 PM
Welcome to the forums.

Give all of the checkboxes a name attribute, then you can access them using syntax like this: document.forms[0].foo. This is where foo is what you named all of the checkboxes.

swatid18
01-14-2004, 12:06 AM
The problem is that my page is a dynamic one, and the checkbox name also varies(it is : chkBxName<Counter>) where the Counter can be 0,1,2....

Originally posted by fredmv
Welcome to the forums.

Give all of the checkboxes a name attribute, then you can access them using syntax like this: document.forms[0].foo. This is where foo is what you named all of the checkboxes.

Pittimann
01-14-2004, 01:10 AM
Hi!

I am just posting this because you said, it's urgent and fredmv (he would give you 1 or 2 lines of code doing what you want) seems to be off.

Take this for a time being:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var allInp;
var allCbs=0;
function check(){
var checkedCBs=new Array();
var unCheckedCBs=new Array();
var checkd=0;
var uncheckd=0;
allInp=document.forms[0].elements.length;
allCbs=0;
for (var i = 0; i < allInp; i++){
if (document.forms[0].elements[i].name.substring(0,9)=="chkBxName"){
allCbs++;
}
}
for (var i = 0; i < allCbs; i++){
if (document.forms[0]["chkBxName"+i].checked==true){
checkedCBs[checkd]=document.forms[0].elements[i].name;
checkd++;
}
else{
unCheckedCBs[uncheckd]=document.forms[0].elements[i].name;
uncheckd++;
}
}
alert('Box(es) checked: ' + checkedCBs+'\n'+'box(es) unchecked: ' + unCheckedCBs);
}
//-->
</script>
</head>
<body>
<form>
<input type="checkbox" name="chkBxName0"><br>
<input type="checkbox" name="chkBxName1"><br>
<input type="checkbox" name="chkBxName2"><br>
<input type="checkbox" name="chkBxName3"><br>
<input type="button" value="check boxes" onclick="check()">
</form>
</body>
</html>

Cheers - Pit

swatid18
01-14-2004, 01:54 AM
The problem with the approach suggested by you is that it looks at all the elements of the page(and thats what i am aiming to avoid for performance related reasons).If i first scan all elements it takes quite some time to execute the javascript.
Is there a way to avoid scanning all the elements?

Originally posted by Pittimann
Hi!

I am just posting this because you said, it's urgent and fredmv (he would give you 1 or 2 lines of code doing what you want) seems to be off.

Take this for a time being:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var allInp;
var allCbs=0;
function check(){
var checkedCBs=new Array();
var unCheckedCBs=new Array();
var checkd=0;
var uncheckd=0;
allInp=document.forms[0].elements.length;
allCbs=0;
for (var i = 0; i < allInp; i++){
if (document.forms[0].elements[i].name.substring(0,9)=="chkBxName"){
allCbs++;
}
}
for (var i = 0; i < allCbs; i++){
if (document.forms[0]["chkBxName"+i].checked==true){
checkedCBs[checkd]=document.forms[0].elements[i].name;
checkd++;
}
else{
unCheckedCBs[uncheckd]=document.forms[0].elements[i].name;
uncheckd++;
}
}
alert('Box(es) checked: ' + checkedCBs+'\n'+'box(es) unchecked: ' + unCheckedCBs);
}
//-->
</script>
</head>
<body>
<form>
<input type="checkbox" name="chkBxName0"><br>
<input type="checkbox" name="chkBxName1"><br>
<input type="checkbox" name="chkBxName2"><br>
<input type="checkbox" name="chkBxName3"><br>
<input type="button" value="check boxes" onclick="check()">
</form>
</body>
</html>

Cheers - Pit

Pittimann
01-14-2004, 02:29 AM
Hi!

If all your formfields are dynamically created, why not also pass the total number of checkboxes as a variable to the document. You can then leave out the loop with elements.length...

Cheers - Pit