0

I m trying to change color using the onclick event. Like on first click RED color ,then on next click Green color,then on another click again backt o RED color. I have completed the click concept but did not understand how to set color concept using JS. Any simple solution?

<head>
    <script>
        function getValue() {
            var x = document.getElementById("myHeader");
            alert(x.innerHTML);
        }
    </script>
</head>

<body>
     <h1 id="myHeader" onclick="getValue()">Click me!</h1>

</body>

VMai
  • 10,156
  • 9
  • 25
  • 34
user3851652
  • 27
  • 1
  • 1
  • 7

1 Answers1

1

I did a small example using fiddle

function display() {
  var colours = new Array();
  colours[0] = "red";
  colours[1] = "blue";
  colours[2] = "green";
  colours[3] = "lime";
  colours[4] = "teal";

  var b = Math.floor(Math.random()*colours.length);
  document.getElementById("quotation").style.color = colours[b];
}

And also you can use colours as follows,

var colours = ["#FF0000","#00FF00","#0000FF","#333399",...];
Sameera Thilakasiri
  • 9,452
  • 10
  • 51
  • 86