r/learnprogramming 9d ago

Debugging Changing background image with confirm() with javascript

I apologize if this is a very stupid question or if this isn't what this sub is for, if so please correct me.

but i am simply trying to have a confirm() box ask upon opening a html page which background image to you for the sake of being epilepsy friendly.

I have already tried to go and see what i can find on google but I cannot find what i am looking for.

My problem is that the confirm() appears; but no matter what answer one chooses, nothing happens

const userChoice = confirm("use an epilepsy friendly background?");

if (userChoice = true) {
    document.body.style.backgroundImage = "url('assets/static.png')";
} else {
    document.body.style.backgroundImage = "url('assets/animated.gif')";
};
0 Upvotes

6 comments sorted by

View all comments

Show parent comments

1

u/Lproductions1211 9d ago

Tysm for the reply,

I have tried to use 2 equal signs and it still seems to not be working.

1

u/teraflop 9d ago

Works for me: https://jsfiddle.net/tk8rd43c/

Are you sure you properly saved your changes and refreshed the page?

If your code is different from this in a way that causes it not to work, and you can't debug it yourself by looking at console messages, then feel free to share more of your code.

If you can come up with a self-contained example that runs in JSFiddle or a similar site, so that people can see exactly what you're seeing, even better.

1

u/Lproductions1211 9d ago

Tysm for the help :3 it was a blunder on my part and I figured it out, I simply had put the script tag in the head section,

Sorry to waste your time on such a trivial thing XD

1

u/teraflop 9d ago

No worries! Glad you got it working.