Click to See Complete Forum and Search --> : changing object properties with a variable.. sorta


iamtheantipop
07-28-2003, 11:11 PM
i dont know javascript terms very well and frequently interchange them so i apologize in advance.

an example of what i am trying to do is this

i have 5 <div id="thingX"></div> objects on my page, where x is an incrementing number between 1 and 5

inside those tags is the letter e. in other words what gets displayed on the page is

e e e e e

now, in javascript what i have is a function similar to the following, in the attempts to change the e's to a's

function changePage(whichThing){

var tempoVar = "thing" + k
tempoVar.innerTEXT = "a"
k++;
if(k<5){
setTimeout("changePage(k);", 1);
}


}

the trouble is, javascript doesn't seem to like it when i want to refer to an object by using a variable to replace the object, churning out "undefined" and the like.

so what i am asking is, how can i refer to an object, without actually directly typing the real object's name myself. i imagine that without such functionality javascript would be very limited, so i'm going to assume that i'm just missing a fundamental aspect of js programming

any help is very much appreciated :)

-chris-

Kvang
07-29-2003, 12:30 AM
var tempoVar="thing"+k;

Try this document.all.tempoVar.innerText="a";
or
document.getElementById(tempoVar).innerText="a";
or
document.all.tempoVar.innerHTML="a";

or you can try

var divobj=document.all.tempoVar;

then do this divobj.innerText="a";

Hope that help and solve your problem.

:)

iamtheantipop
07-29-2003, 12:56 PM
you know the funny thing about this is i use the document.getElementById thing in an xml database i have EXTENSIVELY, so i dont know why i never thought to try that.

the other ones don't work though (document.all.tempoVar) and I think it's because you can't put a variable name inside one of those things. it's looking for a div named tempoVar instead of one that's name with the value in the variable tempoVar itself (thing1, thing2, etc)

but thanks a lot.. i was damn near close to throwing js away completely :)

-chris-