0

Hi there. I have a HTML page with multiple dynamic check boxes that I select, click "add" and it adds a list to the users on the next page.

This is my JavaScript function that deals with this:

<script language="Javascript">
function doSelect() {

  //to avoid "unidentified" results, search elements by tag name
   var myLinkCollection = document.getElementsByTagName("input");
   var formToSend = document.getElementById("addForm"); 
   
   //loop through all elements, looking for a checked checkboxes
   for ( i = 0; i < myLinkCollection.length; i++ ) 
   {
      if( document.getElementsByTagName("input")[i].type == "checkbox" )
      {        
        if ( document.getElementsByTagName("input")[i].checked )
        {  
            document.getElementById("correspondent_id").value =   document.getElementsByTagName("input")[i].value;
	    document.getElementById("action").value = "Select";
	              
            document.forms[0].submit();	   
        }
        
      }
   }
	
	window.close();
	window.opener.window.location.reload();
	
}
</script>

This works fine in IE.

In firefox, it only submits the first selected checkbox value to the next page, and not the rest.

Please help... I have no idea what I'm doing wrong :(

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by fxm
0

I don't know what else is wrong, but do you realize that by calling document.getElementsByTagName("input") repeatedly, at best your code will be unnecessarily slow and at worst it will fail (when the collection returned is smaller than the one you used to set the limit on i at the beginning?. [That can't happen in this toy example but it is possible in principle.] After the first call, just loop through the collection returned - that's what it's for.

0

Ok, I agree... here is simplified version:

<script language="Javascript">
function doSelect() {

  var chboxes = document.getElementsByName("chbox");
  
  for(var i = 0; i < chboxes.length; i++)
  {
     if (chboxes[i].checked)
     {
        document.getElementById("correspondent_id").value = chboxes[i].value;
        document.getElementById("action").value = "Select";
        document.forms[0].submit();
      }
    }
  
    window.close();
    window.opener.window.location.reload();
}
</script>

It still works fine in IE (submits all checked check boxes to the next form) and it doesn't work in firefox (it submits only first checked checkbox to the next form.)

:(

0

The next thing I notice is that you var formToSend = document.getElementById("addForm"); but then you document.forms[0].submit(); . There is no guarantee that those references are to the same form.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.