He estado tratando de usar una función para cambiar un color de fondo con DOM. Probablemente me estoy perdiendo algo súper "en mi cara" pero no puedo verlo
Mi código JS es -
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.Aquí hay un ejemplo de lo que podría hacer, cargue el CSS en el <head> y cargue el JavaScript al final de la página.
<!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>