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
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Forms and Checkboxes, arrays

    Hi,

    as many before me I am very new at this. But hopefully someone out ther can help :-)

    I have the following script:

    Code:
    <script language="javascript">
    <!--
    	function check() {
    		var theform;
    		var list = "";
    		var first = true;
    		
    		//browser detection
    		if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
    			theform = document.__aspnetForm;
    		}
    		else {
    			theform = document.forms["__aspnetForm"];
    		}
    		
    		
    		
    		for(var i=0; i<theform.elements["box"].length; i++)
    		{
    			if(theform.elements["box"][i].checked) 
    			{
    				if(first)
    				{
    					list = "?"+list+"sem"+i+"="+theform.elements["box"][i].value; 
    					first = false;
    				}
    				else
    				{
    					list = list+"&sem"+i+"="+theform.elements["box"][i].value;
    				}
    			}
    		}
    		
    		
    		if(!first)
    		{
    			list = "<%=RegisterPageName()%>"+list;
    		}
    		else
    		{
    			list = "<%=CurrentPageName()%>";
    		}
    				
    		return list;
    	}
    // -->
    </script>
    This script will traverse a form containing checkboxes and get values based on the checkboxes that are checked.

    The problem with this script is that when the form only has one element, which is a checkbox, it will not return the string with the value. But for 2 or more checkboxes it works OK. Why? ? ?

    I can't say I fully understand the script either :-)

    Appreciate any input, anyone.

    Thx,

    tcuos

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is because if you only have one element then you don't have an array or at least that is how I understand it. Try replacing the part of your code where the for loop is with this code that checks to see if ther are more than one elemens and if it there is it will do the for loop if not then it will just check the one element. Hope this helps and good luck.
    Code:
    if(theform.elements["box"].length>1)
      {
       for(var i=0; i<theform.elements["box"].length; i++)
           {
    	  if(theform.elements["box"][i].checked) 
    	    {
    	     if(first)
    		 {
    		  list = "?"+list+"sem"+i+"="+theform.elements["box"][i].value; 
    		  first = false;
    		 }
    	     else
    		 {
    		  list = list+"&sem"+i+"="+theform.elements["box"][i].value;
    		 }
    	    }
    	 }
       }
     else
       {
        if(theform.elements["box"][0].checked) 
    	    {
    	     list = "?"+list+"sem"+i+"="+theform.elements["box"][0].value; 
    	     first = false;
    	    }
       }

  • #3
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    Hi requestcode,

    thank you for replying.

    I pasted the code you provided and it seems that you are correct. The code behaves just as before when there are more than one checkboxes. Now however, I get a prompt about a runtime error, and if I want to Debug:

    "Error: 'elements.box.0.checked' is null or not an object"

    Any ideas? ? ?

    /tcuos

  • #4
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Hello again,

    I amended the code, replacing ["box"][0] with only ["box"]:
    Code:
    	
    else //when there is only one checkbox and if this is checked
    {
    	if(theform.elements["box"].checked) 
    	{
    		list = "?"+list+"sem"+i+"="+theform.elements["box"].value; 
    		first = false;
    	}
    }
    I am not sure why it works now, but it does :-)

    Thank you for your clue!

    /Tcuos

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Quote Originally Posted by tcuos
    Code:
    //browser detection
    if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
    	theform = document.__aspnetForm;
    }
    else {
    	theform = document.forms["__aspnetForm"];
    }
    FWIW, you don't have to do the above detection. Both ways are correct for any browser.


  •  

    Posting Permissions

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