Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable manipulation question

Status
Not open for further replies.

billy83

Programmer
Oct 20, 2006
4
GR
Hello. I want to do the following. I want to write this
if (document.a0.box.checked==true)
but instead of writing .a0. i want to pass the value of a variable.
For example to have sth like temp='a0'; But if i write this
if (document.temp.box.checked==true), it doesn't work. Any suggestion?
 
You gotta show more of your code so we can get a better understanding of what you have so far. Copy and paste rather than writing it in new so we get an exact copy of what the browser sees.

Lee
 
Well my code is the following. I have cut some of it. I just want to replace all this code by writing a loop. The only thing that changes is one variable(a0-a35, yes i have written it 35 times!).


var id=new Array();

function find(){
j=0;
count=0;

if (document.a0.box.checked==true){
id[j]=document.a0.box.value;j++;}count++;
if (document.a1.box.checked==true){
id[j]=document.a1.box.value;j++;}count++;
if (document.a2.box.checked==true){
id[j]=document.a2.box.value;j++;}count++;
if (document.a3.box.checked==true){
id[j]=document.a3.box.value;j++;}count++;

}
 
.box? what is that? if the elements have an ID of "a1", "a2", etc., try something like:

Code:
for ( var i = 0; i < 35; i++ ) {
    if ( document.getElementById("a" + i).checked )
        id[j] = document.getElementById("a" + i).value;
}



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
assuming a0 is the formname, and box is the name of one of the form elements, then you really should be using the form and elements collections:
Code:
if(document.forms["a0"].elements["box"].checked)
and as far as using them in variable form:
Code:
var formName = "a0";
var elementName = "box";
if (document.forms[formName].elements[elementName].checked)

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
You mean something like:
Code:
var id=new Array();

function find(){
var count=0, limit=35;

for (var j=0;j<=limit;j++)
  {
  if (document.forms['a' + j].box.checked==true)
    {
    id[j]=document.forms['a' + j].box.value;
    }
  count++;
  }

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top