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

Recommended Answers

All 3 Replies

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.

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.)

:(

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.