Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
Im trying to change the background of a CSS "body" property with JS...I keep getting a console error of "newBgColor is not defined"

I have been trying to use a function to change a background color with DOM. I'm probably missing something super "in my face" but I can't see it

My JS code is -

function changeBg()     {     
let newBgColor = document.querySelector("body")      
console.log(newBgColor)      
newBgColor.style.backgroundColor = "white";      
}
The CSS Im hoping to change is -

     body {      
  font-family: 'Open Sans';     
  font-weight: normal;      
  max-width: 760px;      
  background-color: #ffe8d6;      
  margin: 0 auto;      
  color: rgb(193, 178, 164);      

  }
``   
I keep getting this error -
ReferenceError - newBgColor is not defined.
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here is an example of what you could do, load the CSS in the <head> and load the JavaScript at the end of the page.

<!DOCTYPE html>
<html>

<head>
  <title>Body Bg Color</title>

  <style>
    body {
      font-family: 'Open Sans', sans-serif;
      font-weight: normal;
      max-width: 760px;
      background-color: #ffe8d6;
      margin: 0 auto;
      color: rgb(193, 178, 164);
      width: 100vw;
      height: 100vh;
    }
  </style>

</head>

<body>


  <script>
    function beigeBg() {
      let newBgColor = document.querySelector("body")
      console.log(newBgColor)
      newBgColor.style.backgroundColor = "beige";
    }

    beigeBg();
  </script>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda