Re: script functioncall question



"WebBuilder451" <WebBuilder451@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:588DBF10-F638-4909-838C-B9F1DC17A1C7@xxxxxxxxxxxxxxxx
i have a functioning script, but i want to make it a little more generic
i just excludes some checkboxes after a specific number are checked.
I'm passing in "this" and it finds it, and it knws it has an id, the id is
correct, but it's not being reconized as a checkbox(list).
i've tried passing it in directly as excludeboxes(3,
document.forms[0].cb1)
and still can't find it.
I'm a dot.net guy here, so i'm sure 'm running into some language issues.
if this were a dot.net i'd just pass the checkbox as an argument and this
is
what i'm trying to do.

Thanks

script
function excludeboxes(iMax, cb) {
alert(cb.length);
len = document.forms[0].cb1.length;
iCnt = 0;
for (i = 0; i < len; i++) {
if (document.forms[0].cb1[i].checked) {
iCnt +=1;
}
}
if (iCnt >= iMax) {
//alert(' reached limit of' + iMax + ' **' + iCnt);
for (i = 0; i < len; i++) {
if (!document.forms[0].cb1[i].checked)
document.forms[0].cb1[i].disabled = true;
}
}
else {
for (i = 0; i < len; i++)
document.forms[0].cb1[i].disabled = false;
}
}

and the html
<form action="" method="post">

<table>
<tr><td>Check Boxes</td><td><input id="cb1" type="checkbox"
onclick="excludeboxes(3, document.forms[0].cb1);" value="1" /></td></tr>
<tr><td></td><td><input type="checkbox" id="cb1" value="2"
onclick="excludeboxes(3,this);" /></td></tr>
<tr><td></td><td><input type="checkbox" id="cb1" value="3"
onclick="excludeboxes(3, this);" /></td></tr>
<tr><td></td><td><input type="checkbox" id="cb1" value="4"
onclick="excludeboxes(3, this);" /></td></tr>
<tr><td></td><td><input type="checkbox" id="cb1" value="5"
onclick="excludeboxes(3, this);" /></td></tr>
<tr><td></td><td><input type="checkbox" id="cb1" value="6"
onclick="excludeboxes(3, this);" /></td></tr>
<tr><td><input type="submit" value="go" /></td><td><input type="reset"
/></td></tr>
</table>
</form>
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)


I'm not sure how you assess C# being cooler that VB when it appears you've
managed to confuse client-side Javascript code with C#.

Here is the function you are after:-

function excludeboxes(iMax, cb)
{

var checkBoxes = cb.form[cb.id]
var len = checkBoxes.length;
var iCnt = 0;
for (var i = 0; i < len; i++)
{
if (checkBoxes[i].checked)
iCnt +=1;
}
if (iCnt >= iMax)
{
//alert(' reached limit of' + iMax + ' **' + iCnt);
for (var i = 0; i < len; i++)
{
if (!checkBoxes[i].checked)
checkBoxes[i].disabled = true;
}
}
else
{
for (var i = 0; i < len; i++)
checkBoxes[i].disabled = false;
}
}

Note use var on variables that you intend to have local scope else they'll
end up in the global scope and cause all sorts of mayhem.


--
Anthony Jones - MVP ASP/ASP.NET


.


Loading