[thelist] Javascript: altering BG color of multiple divs with one click

Hershel Robinson hershelr at netvision.net.il
Tue Nov 11 10:42:16 CST 2003


> I'd also like to be able to click on a div and have it alter 
> the color of other
> divs. For example, clicking on Div 3 would alter its color 
> and that of Div 1.

Put all the DIVs in a container div with id="Container"

Add to each inner DIV onclick="setColors(this);"

Then something like this:

function setColors(newSpecial) {
	// set all DIVs to default
 	myKids = document.getElementById("Container").getElementsByTagName("DIV")
	for (var i=0; i<myKids.length; i++) {
		myKids[i].className = 'default'
	}
	// set new one to special color
	document.getElementById("newSpecial").className = "SpecialClass"
}

HTH

Hershel


More information about the thelist mailing list