-3

I wanted to change bg color in this order: [Black,White,Red,Green,Blue]

<body style="background-color:black">

    <script type="text/javascript">
        function test() {
            var bg = document.bgColor;
            var e = document.getElementById("tests");

            if (bg == "black")  {e.style.backgroundColor = "white";}
            else if (bg == "white") {e.style.backgroundColor = "red";}
            else if (bg == "red") {e.style.backgroundColor = "green";}
            else if (bg == "green") {e.style.backgroundColor = "blue";}
            else if (bg == "blue") {"<a href="/testson"/>";}
                        }

    </script>
    <div class="test"; onclick="test()"></div>
</body>

But when I click somewhere on page, nothing happens.

Suppy
  • 11
  • 2
  • 1
    `div` does not have `onclick` attribute – Adam Azad Nov 29 '17 at 19:27
  • 1
    How do you click on an empty `div`? Why is there a semicolon in your `
    ` element? What do you expect `document.getElementById("tests")` to return? What does anything in your question have to do with your title? What does your browser's debugging console tell you? (Hint: It's showing you errors.)
    – David Nov 29 '17 at 19:28
  • Possible duplicate of [How to redirect to another webpage?](https://stackoverflow.com/questions/503093/how-to-redirect-to-another-webpage) – DrNio Nov 29 '17 at 19:29
  • @AdamAzad — All HTML elements support the `onclick` attribuet – Quentin Nov 29 '17 at 19:48
  • put some content in .test div – Muhammad Usman Nov 29 '17 at 19:51

2 Answers2

0

you have to do like this

 // similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

remove ; from html

 <div class="test" onclick="test()">Click me!!</div>
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
-1

The reason why your code is not working is that div tags do not support the action onclick. In order to make the div clickable, you would need to set an ID for the div and then in javascript assign an event handler to process the click for example:

<head>
    <script type="text/javascript">
        function test() {
            var bg = document.bgColor;
            var e = document.getElementById("tests");

            if (bg == "black")  {
                e.style.backgroundColor = "white";
            }
            else if (bg == "white") {
                e.style.backgroundColor = "red";
            }
            else if (bg == "red") {
                e.style.backgroundColor = "green";
            }
            else if (bg == "green") {
                e.style.backgroundColor = "blue";
            }
            else if (bg == "blue") {
                "<a href="/testson"/>";
            }
        }
        function AddHandlers(){
            var something = document.getElementById('someid');

            something.style.cursor = 'pointer';
            something.onclick = function() {
                 test();
            };
        }
    </script>
</head>

<body onload="AddHandlers()">
    <div class="test" id="someid">Click me!!</div>
</body>
Marcello B.
  • 4,177
  • 11
  • 45
  • 65