tags:

views:

37

answers:

1

I'm trying to create an if...else if...else statement in Javascript that will change the style of a specific CSS ID based on a specific number. The if...else if...else code isn't the problem, it's just that as a Javascript novice (putting it lightly...) I'm not sure where to add the CSS code to execute it. From what I understand I would put it in the areas I designated as "CSS CODE" below (sorry for the poorly formated code).

if (Number == 9) {CSS CODE} else if (Number == 8) {CSS CODE}

I looked at another question here and the code that was provided to achieve this is below.

var element = document.getElementById("ElementID");  
element.style.backgroundColor = "#fff";

When I placed the var at the top and the element.style.etc within the CSS CODE section it did not work properly.

As a side note I need all the styling to be contained within the script, so adding a new class to it won't work. Along the same line of thought I can't use jQuery for any of this or I would have already :)

+1  A: 

This doesn't work?

    var element = document.getElementById("ElementID");
    var Number = 8;
    if (Number == 9) {
        element.style.backgroundColor = "#fff";
    } else if (Number == 8) {
        element.style.backgroundColor = "#000";
    }
Floyd Pink
Nope. That's how I had it set up originally.
Edvard D
What browser(s) are you testing in? Does it fail in all browsers or just one? Are you sure that "ElementID" is the correct ID and is unique in your document? And is Javascript enabled? Have you tried stepping through the code in Firebug? Does it throw a Javascript error? The code given looks fine as it stands, so I don't know what else to suggest.
Spudley