...

View Full Version : Passing CheckBox to a method



Parijat
02-11-2004, 01:38 PM
hi i have a html page in which a link calls a method and passes the name of list of checkboxes sharing the same name .Now within that method i want to know the no of checkboxes
i.e something like checkBoxname.length .
Can someone help me with the code as it is very urgent :-

RadarBob
02-11-2004, 02:53 PM
You're on the right track. When you have a set of checkboxes all with the same name, you get an array of all those checkboxes when you reference that name.

In JavaScript, since it's an array, it automatically has a "length" attribute. Just use it.

Parijat
02-12-2004, 05:59 AM
hi ,
actually the problem is that in the method i am passing the name of the set of checkbox in my case the name is variableNameCheck
and i call the method as :
javascript:checkAll('variableNameCheck')
now in the method :
function checkAll(obj)
{

alert(document.forms[0].obj.length);

}

it gives me an undefined in the alertbox .
hope u r getting my problem.pls help urgently

Willy Duitt
02-12-2004, 06:39 AM
Does this help?
<script type="text/javascript">
function checkAll(obj){
alert(document.forms[0][obj.name].length);
}
</script>
</HEAD>

<BODY>
<form>
<input type="checkbox" name="chk" onclick="checkAll(this)">Check 1<br>
<input type="checkbox" name="chk" onclick="checkAll(this)">Check 2<br>
<input type="checkbox" name="chk" onclick="checkAll(this)">Check 3<br>
<input type="checkbox" name="chk" onclick="checkAll(this)">Check 4<br>
<input type="checkbox" name="chk_2" onclick="checkAll(this)">Check A<br>
<input type="checkbox" name="chk_2" onclick="checkAll(this)">Check B<br>
<input type="checkbox" name="chk_2" onclick="checkAll(this)">Check C<br>

</form>

.....Willy

glenngv
02-12-2004, 06:55 AM
function checkAll(chkName)
{
alert(document.forms[0].elements[chkName].length);//test
var objChks = document.forms[0].elements[chkName];
for (var j=0;j<objChks.length;i++){
objChks[i].checked = true;
}
}


If the checkbox is dynamically generated by a server-side script and there is a chance that only one checkbox (or none at all) will exist, the code should be like this:



function checkAll(chkName)
{
var objChks = document.forms[0].elements[chkName];
if (objChks) {
if (typeof objChks.length != 'undefined'){ //multiple checkboxes
for (var j=0;j<objChks.length;i++){
objChks[i].checked = true;
}
}
else objChks.checked = true; //only one checkbox
}
}

RadarBob
02-12-2004, 01:58 PM
Originally posted by Parijat
hi ,
actually the problem is that in the method i am passing the name of the set of checkbox in my case the name is variableNameCheck
and i call the method as :
javascript:checkAll('variableNameCheck')
If nothing else, you're passing a string with the value "variableNameCheck", you are not passing the thing/object variableNameCheck. Take out the quotes.

glenngv
02-13-2004, 02:42 AM
Parijat is passing the name of the checkboxes not the checbox object.


hi ,
actually the problem is that in the method i am passing the name of the set of checkbox in my case the name is variableNameCheck
and i call the method as :
java script:checkAll('variableNameCheck')
now in the method :
function checkAll(obj)
{

alert(document.forms[0].obj.length);

}

it gives me an undefined in the alertbox .
hope u r getting my problem.pls help urgently


Parijat just used a misleading parameter name obj but he/she meant checkbox name. And I correct it in the code I posted above.

But if variableNameCheck is actually a variable that holds the name of the checkboxes then there should be indeed no quotes when you pass it in checkAll() function. But if the name of the checkboxes is literally variableNameCheck, then the quotes are required.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum