/    Sign up×
Community /Pin to ProfileBookmark

Change the color of several elements in the page

Damn – session timeout – I have to type this over again.

Already tried this:

I created a table with 5 rows and gave them all the same ID attribute, and hoped that with:

document.getElementById(“that_id”).style.background = “#CCCCCC”;

would change the background of all of them, but only the first was changed ( tried SetTimeout on the function – didn’t help )

1) So, Anyone know’s how to do this, sample code is always welcome ?

2) Is there a way to find all of the ID attributes on the page or inside one element ( and inside it there are several <SPAN> or <DIV> tags with their ID’s which I want to collect )

Thanks Ahead!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsJan 16.2005 — yes, but i doubt its necessary

give your table an id say 'MyTable';

then in the <body> tag onload="FindTableTRs('MyTable');"


then (or variations of)
[code=php]
<script>

function FindTableTRs(Obj){
TRs=getElementById(Obj).getElementsByTagName('TR');
for (i=0;i<TRs.length;i++){
TRs[i].style.backgroundColor='red';
}
TRs[0].style.backgroundColor='blue';
}


</script>
[/code]

now the first row will be blue and all other rows red and you can use

TRs[n] instead of an id
Copy linkTweet thisAlerts:
@hastalavistaJan 16.2005 — You may not use the same id multiple times in one document.

Only the first occurance will get the id.

The have multiple elements to get the same style use classes.

On 1)

Why do you try to style the rows? Isn't it more straightforward to style the table? You could set the background of anything in the table to transparent to avoid 'painting-over'.

As styling tables is done in different ways by various (older) brwosers, you may want a saver way.

You could set the background of the top layer in the table: the cells. Put the cells in the same class, nand let that have a background color.

Also, when you need to change the background later, you could let them all have a unique id. If that is your plan, my experience is that it is best to set an initial value in the tag itself.

example:

function specialevent() {

document.getElementById('cell6').style.background = 'navy';

document.getElementById('cell7').style.background = 'navy';

}

.......


<td id="cell7" style="background: yellow;" onmouseover="this.style.background='gray';" onclick="specialevent();">party item</td>

2)

You could make an array with document.getElementsByTagName('td'); and the test the id or className of the arraymembers.
×

Success!

Help @Calimero 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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