Javascript statement variables

I would like to replace the following javascript code:

switch (j) {

case 1 :

  var valuedat=document.forms(0).dat1.value;

  break;

case 2 :

  var valuedat=document.forms(0).dat2.value;

  break;

case 3 :

  var valuedat=document.forms(0).dat3.value;

  break;

case 4 :

  var valuedat=document.forms(0).dat4.value;

  break;

case 5 :

  var valuedat=document.forms(0).dat5.value;

  break;

case 6 :

  var valuedat=document.forms(0).dat6.value;

  break;

case 7 :

  var valuedat=document.forms(0).dat7.value;

  break;

default :

}

with one statement by simply using the variable j in the assign statement as you would in lotus script. For example something like:

var valuedat=document.forms(0).dat + j +.value

How do I do it?

Thank you

Subject: document.forms[0].elements[‘dat’ + j].value

Subject: javascript statement variables

Don’t take this as gospel, because I’m still learning javascript myself, but I think you should be able to use eval() - so your line should be:var valuedat=eval(document.forms(0).dat + j +.value)

hth

Tony

Subject: RE: javascript statement variables

And don’t take this the wrong way either, Tony, but there are usually better ways of doing things than to use eval(). (This is educational, not confrontational, in intent.)

In this case, you wouldn’t be able to tell the difference – it’s immeasurablly small, since it’s only done once – but if it were to occur in a loop, the whole statement needs to be constructed, then examined to determine what it’s supposed to do, then executed for every iteration.

The alternate method uses “set in concrete” methods belonging to a certain class of object – only the identity of the object changes, using a name that is passed in as a parameter. The interpreter can set up the low-level code that needs to be performed one time, then feed a string in one and and get a different string out the other.

The only real “secret” is in learning the various arrays that are contained in a document object, which elements are available through which arrays, and whether string or index addressing (document.forms[‘formname’].elements[‘fieldname’] or document.forms[0].elements[5]), or a combination is best for the situation. If you ever learn it all, let me know – there are a few things I’m sure I’m missing.

Subject: RE: javascript statement variables

Thanks Stan for that very constructive response. It’s taken entirely as meant - as I said, I’m still very much learning. I’ve just checked back in Danny Goodman’s JavaScript Bible, and he does give that warning, also the better way that you preached. I just hadn’t got round to reading that far - just far enough to get it working :frowning:

Tony

Subject: RE: javascript statement variables

Stick with Danny and you’ll go places. I like his stuff, mostly because it doesn’t dwell on the theoretical – everything he writes is tested. If you don’t mind carrying a 1400-page book with you everywhere you go, you’ll find his “Dynamic HTML – The Definitive Reference (2nd Edition)” is worth its considerable weight in some precious metal or other. It ain’t a pocket guide, to be sure, but you’ll probably find that if you’re given a choice between carrying that or your umbrella to a work site, you’d rather be wet.