How do I change the background color in JavaScript?

A.D.Schmidt

I am trying to change the background color of the wrapper every time I click the button. When it is clicked, it is supposed to change to a random color that is in the array. Any thoughts for a beginner? Also the CSS was done with SASS so it may not look pretty.

function change_Color() {
  var color = ["blue", "red", "green", "yellow"];
  document.getElementById("wrapper").style.backgroundColor = color;
}
body {
  margin: 0;
}

* {
  box-sizing: border-box;
}

#wrapper {
  width: 100%;
  height: 100vh;
  background-color: skyblue;
  display: flex;
  justify-content: center;
  align-items: center;
  button {
    padding: 10px;
    background: orange;
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 2rem;
    transition: .5s ease;
    &:hover {
      box-shadow: 0 4px 3px 0px rgba(255, 255, 255, 0.8);
      cursor: pointer;
    }
    &:focus {
      outline: 0;
      color: skyblue;
    }
  }
}
<div id="wrapper">
  <button onclick="change_Color()">Click me!</button>
</div>

Alyona Yavorska

Check this one. I merged body and wrapper in CSS and I gave id for body

var colors = ["red", "blue", "green", "yellow"];
    function changeColor() {
        document.getElementById("body").style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];}
#body {
margin: 0;
box-sizing: border-box;
  width: 100%;
  height: 100vh;
  background-color: skyblue;
  display: flex;
  justify-content: center;
  align-items: center;
}
 button {
    padding: 10px;
    background: orange;
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 2rem;
    transition: .5s ease;
    &:hover {
      box-shadow: 0 4px 3px 0px rgba(255, 255, 255, 0.8);
      cursor: pointer;
    }
    &:focus {
      outline: 0;
      color: skyblue;
    }
  }
<body id='body'>
    <button onclick="changeColor();">Click me!</button>
    </body>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I change the background color with JavaScript?

How do I change the background color of an element on drop with JavaScript

How do I change the background color of the body?

How do I change a background color in JS?

How do I get the event listener to change my css background color using javascript?

How do I change the background color of a QtQuick Window?

How do i change input field background color based on onclick?

How do I change the background color of a tabbed page in Xamarin iOS?

how do I change my header background color?

How do I change the background color of a unicode symbol?

How do I change the entire background color in an RMarkdown HTML Document?

How do I change button background and text color?

How do I change the background color of a cell in Pandas?

How do I change the ripple background color on Button?

How do I change the vuetify theme background color

How do I change the Windows 8.1 background color?

How do I change the background color of the ActionBar of an ActionBarActivity using XML?

How do I change the background color of a JFrame dynamically?

How do I change the background color of a button in Kivy?

How do I change a tab background color when using TabLayout?

How do I change dynamically a background color of a WPF:s button

How do I change the color of my background smoothly?

How do I change xterm's menu background color?

How do I change the background color of an iron-icon in Polymer?

How do I change the background of a paragraph to a random color on click with jQuery?

How do I change the background-color of an accordion tab in Bootstrap?

How do I change the background color of calendar dates in MaterialCalendarView

MFC: How do I change the background color of the whole editBox?

How do I change the background color of a cell by a condition?