/    Sign up×
Community /Pin to ProfileBookmark

changing object properties with a variable.. sorta

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-

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KvangJul 29.2003 — 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.

?
Copy linkTweet thisAlerts:
@iamtheantipopauthorJul 29.2003 — 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-
×

Success!

Help @iamtheantipop spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...