Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can i do this?

Status
Not open for further replies.

iostream71

Programmer
Mar 13, 2002
229
US
say i have a bunch of divs of a particular class. i want to click a button and change an attribute of that class for all the div instances. how could i do that with javascript?

.myCoolDiv{
background-color:red;
}
.
.
.
<div class=&quot;myCoolDiv&quot;>blah</div>
<div class=&quot;myCoolDiv&quot;>more blah</div>
< button to change div background color to &quot;blue&quot;>

 
It is possible, but you have to do the following:

1. add unique ID to all divs:
<div class=&quot;myCoolDiv&quot; id=&quot;div1&quot;> - first one
...
<div class=&quot;myCoolDiv&quot; id=&quot;div15&quot;> - last one

2. add this function to the <head>:

<script>
function changeColor() {

for (n=1; n<16; n++)
eval(&quot;document.getElementById('div&quot; + n + &quot;').style.backgroundColor = 'blue'&quot;);
}
</script>


That's it.
 
i found another way that might be the easiest. it involves using alternate style sheets
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top