Page 1 of 1

incorporate variable into object names

Posted: Sun Dec 13, 2009 5:38 am
by finchamgroves
Hi
I am learning how to access other windows using JavaScript and the code below works just fine but what I would like to do is run through a for loop that changes a variable car into car1 to car2, car 3 etc - BUT can't work out how to incorporate the variable car into the places where I currently have car1 into the code block?
Any help very welcome!

Code: Select all

 
<script type="text/javascript">
// This function checked whether the car is already checked on the main window or not.
// Then, it toggles the status of the checkbox accordingly
function process() 
{
//want a for loop that replaces car1 in the code below into car1, car2, car3 ....  but how ?
 
   if (opener.document.forms[0].car1.checked) 
   {
      opener.document.forms[0].car1.checked = false;
      document.getElementById("car1").innerHTML="[add]";
   }
   else {
      opener.document.forms[0].car1.checked = true;
      document.getElementById("car1").innerHTML="[remove]";      
   }
}
</script>
 

Re: incorporate variable into object names

Posted: Sun Dec 13, 2009 2:55 pm
by finchamgroves
OK - Have sorted this by another method whilst waiting for a response.
I'll put it here as it may be useful to other newbies

Code: Select all

 
<script type="text/javascript">
// This function checked whether the car is already checked on the main window or not.
// Then, it toggles the status of the checkbox accordingl
 
function process(ref) 
{  
            if (opener.document.ChooseCar.elements[ref].checked) //is it currently checked?
               {
                  opener.document.ChooseCar.elements[ref].checked = false; //change to unchecked following the click
                  document.getElementById(ref).innerHTML="[add]"; //as now unchecked offer add option
               }
            else
                {
                opener.document.ChooseCar.elements[ref].checked = true; //change to checked
                 document.getElementById(ref).innerHTML="[remove]"; //as now checked offer the remove option        
                  }
} 
</script>
 
Despite the lack of reply it's good to have this site available as - in the end - someone always helps out. :D