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

Hershel Robinson hershelr at netvision.net.il
Tue Nov 11 13:53:32 CST 2003


> I discovered the problem with my code was that this:
>
> (i != 0)
>
> crashed my browsers, while this:
>
> if (i != 0)
>
> doesn't.
>
> Eg. This works:
>
> for (var i = 0; i < relatedItems.length; i++)
>   {
>   if (i != 0)
>     {
>     document.getElementById(relatedItems[i]).className = "question";
>     }
>  else
>     {
>     document.getElementById(relatedItems[i]).className = "answer";
>     }
>   )
>
>
> Very odd :o/

Not sure why you find this odd. Most languages require the keyword 'if'
before an if/else statement.

I would actually clean up the above code to this:
for (var i = 0; i < relatedItems.length; i++)
	document.getElementById(relatedItems[i]).className = (i != 0) ? "question"
: "answer"

:)

Hershel



More information about the thelist mailing list