/    Sign up×
Community /Pin to ProfileBookmark

How to call dymanically an id (span, td, etc) ?

Hi.
I’m french then my problem is not easy to explain (for me).

I’ve a some td in a table that i want to change bgcolor.
then i’ve named my td as :

<td id=”td1″ align…..
<td id=”td2″ align…..
etc

then i can do :
td_1.bgColor=’f0f0f0′;

but for some reason i need to call dynamically the name of the td. i’ve tried this :

my_number = comes from a function will be 1,2, etc;
[“td” + my_number].bgColor=’ffffff’;

and this doesn’t work. i’ve put “document.” before but nothing better.

do you know the correct syntax for this ? thanks

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@GollumJun 12.2003 — try

document.getElementById("td" + my_number).bgColor
Copy linkTweet thisAlerts:
@BougliauthorJun 12.2003 — this seems to work !

a BIG thanks !
Copy linkTweet thisAlerts:
@Khalid_AliJun 12.2003 — [i]Originally posted by Gollum [/i]

[B]try



document.getElementById("td" + my_number).bgColor [/B]
[/QUOTE]


If the above works then it must only work in IE

it should be

document.getElementById("td" + my_number).style.backgroundColor="green"

Reason being that TD does not have bgColor attribute in any future versions of it.(it will only work in transitional DTDs)
×

Success!

Help @Bougli 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.28,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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