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
  • 9,978
  • 9
  • 23
  • 34
user3851652
  • 27
  • 1
  • 1
  • 7
  • 1
    Check this question http://stackoverflow.com/questions/195951/change-an-elements-css-class-with-javascript?rq=1 – Klaster_1 Sep 18 '14 at 05:42
  • in simple word i want to change color of "click me " on every click the "click me " color change its may be three color or more and repeated....@Klaster_1 – user3851652 Sep 18 '14 at 05:51

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,255
  • 9
  • 53
  • 84