T.T.T.
T.T.T.

Reputation: 34553

How do I change the name of multiple selected html values?

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' 

Upvotes: 0

Views: 258

Answers (3)

T.T.T.
T.T.T.

Reputation: 34553

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

}

Upvotes: 0

Richard A
Richard A

Reputation: 2833

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.

Upvotes: 1

mati
mati

Reputation: 5348

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();
    }

Upvotes: 1

Related Questions