...

View Full Version : checkbox count is undefined



chelvis
12-13-2006, 04:42 PM
I have the following code.

When I have multiple checkboxes the following code works fine. But if I have only one check box, then I even when I check that one checkbox I get the alert as " check atleast one checkbox". I am having this situation because sometimes I have multiple but other times I only have one checkbox (comes from backend code). How to fix this?

When I have only one checkbox, when the user checks it I want it to open a new window. But it still gives the alert


<html>
<head>
<title>Untitled</title>
<script Language="JavaScript">

<!--
function validate_checkbox()
{


var checkboxSelect = 0;


for (counter = 0; counter < window.document.forms["frmSpecs"].Check1.length; counter++)
{
if (window.document.forms["frmSpecs"].Check1[counter].checked)
{
checkboxSelect = checkboxSelect + 1;
}
}

if (checkboxSelect > 3 )
{

alert("You are limited to only three selections")
return (false);
}

if (checkboxSelect == 0 )
{

alert("Please select atleast one")
return (false);
}
return true;
}
-->
</script>
</head>
<body>
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="10"><img src="../common/images/spacer.gif" height="1" width="10" alt="" border=0/></td>
<td valign="top">

<form name="frmSpecs" id="CategorySpecs" method="post">
<input type="checkbox" name="Check1" value="1">word1<br>

<a href='#' onclick ='javascript:validate_checkbox();returnValue:false;'><img src='images/img1.gif' border='0'></a>
</form>

</td>
</tr>
</table></body>
</html>

Brandoe85
12-13-2006, 05:09 PM
Something about if there's only one, the length property isn't returned since 1 isn't an array, or something I can't remember :o
Try:


<html>
<head>
<title>Untitled</title>
<script Language="JavaScript">

<!--
function validate_checkbox()
{


var checkboxSelect = 0;

var mult = (window.document.forms["frmSpecs"].Check1.length != null) ? true : false;
if(mult)
{
for (counter = 0; counter < window.document.forms["frmSpecs"].Check1.length; counter++)
{
if (window.document.forms["frmSpecs"].Check1[counter].checked)
{
checkboxSelect = checkboxSelect + 1;
}
}
}
else
{
if(window.document.forms["frmSpecs"].Check1.checked)
{
checkboxSelect++;
}
}

if (checkboxSelect > 3 )
{

alert("You are limited to only three selections")
return (false);
}

if (checkboxSelect == 0 )
{

alert("Please select atleast one")
return (false);
}
return true;
}
-->
</script>
</head>
<body>
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="10"><img src="../common/images/spacer.gif" height="1" width="10" alt="" border=0/></td>
<td valign="top">

<form name="frmSpecs" id="CategorySpecs" method="post">
<input type="checkbox" name="Check1" value="1">word1<br>

<a href='#' onclick ='javascript:validate_checkbox();returnValue:false;'><img src='images/img1.gif' border='0'></a>
</form>

</td>
</tr>
</table></body>
</html>
Though, I believe this was the same issue and a solution was provided here:
http://www.codingforums.com/showthread.php?t=46121&page=2

Good luck

chelvis
12-13-2006, 05:49 PM
Thanks Brandoe85

I am doing this in the .net environment. So when I click on the image I am getting the following error now. 'checked' is null or not an object.

I get this error only in the .net page. But if I do it with plain html it works fine. Why?

Brandoe85
12-13-2006, 06:05 PM
Hm...what's the generated html of the page that's throwing the error? Also, what browswer? it's working for me with .NET IE6 and FF 2

chelvis
12-13-2006, 06:18 PM
Sorry its my mistake. Its working fine now.
Thanks again Brandoe85



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum