How to add CSS code inside if ... else if ... else Javascript statement

I am trying to create an if ... else if ... else statement in Javascript that will change the style of a specific CSS identifier based on a specific number. The if ... else code if ... else is not a problem, it is just like Javascript beginner (putting it a little ...) I'm not sure where to add CSS code to execute it. From what I understand, I would put it in the areas that I called "CSS CODE" below (sorry for the poorly formed 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 put var at the top and element.style.etc in the CSS CODE section, it did not work properly.

As a side note, I need the whole style to be contained in the script, so adding a new class to it will not work. For the same thought, I cannot use jQuery for any of this or I would already :)

+3
source share
2 answers

This does not work?

    var element = document.getElementById("ElementID");
    var Number = 8;
    if (Number == 9) {
        element.style.backgroundColor = "#fff";
    } else if (Number == 8) {
        element.style.backgroundColor = "#000";
    }
+1
source
var element = document.getElementById("ElementID");
var Number = 1;

switch(Number)
{
case 1:
  element.style.backgroundColor = "#fff";
  break;
case 2:
  element.style.backgroundColor = "#000";
  break;
default:
  element.style.backgroundColor = "#FAB";
  break;
}
0
source

Source: https://habr.com/ru/post/1763824/


All Articles