1

JSFiddle is here: https://jsfiddle.net/2Xgfr/829/

Raise function below works as expected (cycle through colors). Lower function does not and the only difference is the assignment of colors from text to hex.

HTML elements

<button type="button" id="subEmail" onclick="Raise();">Raise</button>
<button type="button" id="subEmail2" onclick="Lower();">Lower</button>

JS piece

<script>
  function Raise() {
    var inputVal = document.getElementById("subEmail");
    var currentcolor = inputVal.style.backgroundColor;
    var color1 = "red";
    var color2 = "yellow";
    var color3 = "blue";
    switch (currentcolor) {
        case color1:
            inputVal.style.backgroundColor = color2;
        break;
        case color2:
            inputVal.style.backgroundColor = color3;
        break;
        default:
            inputVal.style.backgroundColor = color1;
            break;

    }

} // function Lower()
  function Lower() {
    var inputVal = document.getElementById("subEmail2");
    var currentcolor = inputVal.style.backgroundColor;
    var color3 = "#FF0000";
    var color2 = "#FFFF00";
    var color1 = "#0000FF";
    switch (currentcolor) {
        case color1:
            inputVal.style.backgroundColor = color2;
        break;
        case color2:
            inputVal.style.backgroundColor = color3;
        break;
        default:
            inputVal.style.backgroundColor = color1;
            break;

    }

} // function Lower()

</script>

JSFiddle

Andreas
  • 21,535
  • 7
  • 47
  • 56
JoeHerbst
  • 19
  • 2

1 Answers1

0

You are not getting the hexadecimal value properly in this line, so the switchstatement will always evaluate the default.

var currentcolor = inputVal.style.backgroundColor;

Try to log or debug for the value of that variable.

Here you have a great answer on how to do that with javascript.

The rest of the code I think is looking fine.

Community
  • 1
  • 1
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73