loader image

JavaScript program to change background color

Write a JavaScript program to store different colours in an array and change the background colour of the page using these array elements.

<html>

<body id="body">
    <input type="button" onmouseover="changeBg(0)" value="Red">
    <input type="button" onmouseover="changeBg(1)" value="Yellow">
    <input type="button" onmouseover="changeBg(2)" value="Blue">
    <input type="button" onmouseover="changeBg(3)" value="Green">
    <input type="button" onmouseover="changeBg(4)" value="Orange">
    <script>
        function changeBg(i) {
            const colors = ["red", "yellow", "blue", "green", "orange"];
            document.body.style.background = colors[i];
        }
    </script>
</body>

</html>
Output
bgcolor
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top