views:

84

answers:

3

I have about 20 check boxes. When the user selects these and then uses an alternate submit button, I need to change the name of the name/value pair, for the selected inputs.

Why does this function only change the name of every other selected input?

function sub_d()
{

    for (i = 0; i < document.checks.OGname.length; i++) //for all check boxes
    {
     if (document.checks.OGname[i].checked == true)
     {
      document.checks.OGname[i].name="newname"; //change name of input 

     }

    }

    document.checks.submit();
}

The output:

newname
    '105' 
OGname
    '106' 
newname
    '107' 
OGname
    '108' 
newname
    '109' 
OGname
    '110'
+1  A: 

By renaming the first element of the list you have reduced the length of the list by one and deleted the first element. Next time through the loop the previous second element is now the first, and the second is the old third.

I'm no javascript expert, but something along the lines of this might work.

function sub_d()
{
  i=0;
  while (document.checks.OGname.length > i)
  {
    if (document.checks.OGname[i].checked="true")
      {
        document.checks.OGname[i].name="newname";
      }else{
        i++;
      }
  }
  document.checks.submit();
}

As I said, no warranty or guarantee.

Richard A
+1  A: 

Would be great if you provide a more detailed description of your scenario, but I wish that my answer be useful.

function sub_d()
{    
    for (i = 0; i < document.checks.OGname.length; i++) //for all check boxes
    {
        if (document.checks.OGname[i].type == 'CHECKBOX')
            if (document.checks.OGname[i].checked)
               {
                   document.checks.OGname[i].name="newname"; //change name of input 

               }    
    }    
    document.checks.submit();
}

I usually manage dom collections in this way: (I don't know if is the best way)

   function sub_d()
    {    
        var theInputs = document.checks.getElementsByTagName('input');
        for (var i = 0; i < theInputs.length; i++) 
        {
            if (theInputs[i].type == 'CHECKBOX')
                if (theInputs[i].checked)
                   {
                       theInputs[i].name="newname";         
                   }    
        }    
        document.checks.submit();
    }
Matias
Thanks, I changed the code to use the getElementsByName function so I don't have to have extra logic for the length undefined issue below.
Tommy
A: 

With your guys help I came up with this, seems to work well. Let me know if it can be improved for others to use...

function sub_d()
{

    for (i = 0; i < document.checks.OGname.length; i++) //for all check boxes
    {
     if (document.checks.OGname[i].checked == true)
     {
      document.checks.OGname[i].name="newname"; //change name of input data so we know it is for other function
      //By renaming the first element of the list, we have reduced the length of the list by one 
      //and deleted the first element. This is why we need to keep i at it's current position after a name change.
      i=i-1; 
     }
    }

    //When there is only one check box left it's propert length becomes undefined.
    //We will need this statement for the last undefined check box not covered in the for loop
    //We can no longer index user[0]
    document.checks.OGname.name="newname"; 

    document.checks.submit();//submit these checked values to the .exe

}
Tommy
I said I didn't know javascript :) I didn't anticipate the length becoming undefined. I still prefer to use a while, rather than decrementing i within the loop, it just seems clearer to me. Can you use my code, but change the loop condition to exit on length being undefined, then catch that last one? It'd be nice to catch it in the loop, but I might be being a bit precious.
Richard A