Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    406
    Thanks
    0
    Thanked 0 Times in 0 Posts

    checkbox count is undefined

    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

    Code:
    <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>

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    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
    Try:
    Code:
    <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/showthre...t=46121&page=2

    Good luck
    Last edited by Brandoe85; 12-13-2006 at 05:41 PM.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    406
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    406
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry its my mistake. Its working fine now.
    Thanks again Brandoe85


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •