Okay , so iv been stuck on this for a few hours now and have tried everything i could think of / read up on. In my project i have 3 buttons, each button should change the background image of my site. the background image is tied to the default "App" div element in a react project. I am trying to write javascript code that changes the image based on what color is clicked ( the background image is a scooter, only the scooter color is what is intended to change, its not just a blank color covering the background)
Here is my javascript file to change the colors
document.getElementById("green").addEventListener(onclick, changeColorGreen);
document.getElementById("black").addEventListener(onclick, changeColorBlack);
document.getElementById("silver").addEventListener(onclick, changeColorSilver);
let greenurl = "url('../images/GreenScooter')"
let blackurl = "url('../images/GreenScooter')"
let silverurl = "url('../images/GreenScooter')"
export function changeColorGreen() {
document.getElementById("App").style.backgroundImage = greenurl;
}
export function changeColorSilver() {
document.getElementById("App").style.backgroundImage = silverurl;
}
export function changeColorBlack() {
document.getElementById("App").style.backgroundImage = blackurl;
}
and this is my JSX component, well the part that pertains to this anyway:
import { changeColorBlack, changeColorGreen, changeColorSilver } from "./colorSelector";
<div className="d-flex mx-auto pt-5" id="colorselector">
<button type="button" class="btn btn-success mx-2" onClick={changeColorGreen} id="green"></button>
<button type="button" class="btn btn-success mx-2" onClick={changeColorBlack} id="black"></button>
<button type="button" class="btn btn-success mx-2" onClick={changeColorSilver} id="silver"></button>
</div>
</div>
i have tried all the different ways suggested via various stackOverFlow threads, youtube and google. Most of them suggested putting the URL in a variable but it still doesnt work. I tested the code and changed it so that it changes the entire background to the 3 different colors and not images and it works just fine. However trying to get it to change to the different images just wont work. Any help or guidance on the matter would be greatly appreciated thank you



styleattribute that's changed there. Background image should probably be in a state variable.document.getElementById("App").style.backgroundImagecan cause unexpected bugs.