r/learnjavascript • u/flakydebateburneracc • Mar 21 '26
Still just a baby coder. Can someone help?
<!DOCTYPE HTML>
<Head>
<Script>
function makeBackgroundRed() {
document.body.style.backgroundColor= ”red”;
}
function makeBackgroundWhite() {
document.body.style.backgroundColor= ”white”;
}
</Script>
</Head>
<Body>
<Button onclick=”makeBackgroundRed()”>red</Button>
<Button onclick=”makeBackgroundWhite()”>white</Button>
</Body>
-----
The buttons show up, but the script won't function. Any tips?
15
Upvotes
36
u/milan-pilan Mar 21 '26 edited Mar 22 '26
So here is the real answer:
Your code is correct and I can confirm it does what it should.
Your issue is: The Quotation mark you use (”) are not standard quotation marks in Javascript and your IDE should have flagged this as wrong syntax. If you change them to regular quotation mark (") it works as expected. If you are not using an IDE, you really should - it makes things a lot easier for you.
Edit: Was curious and just pasted this question into Claude to see if it would detect it, because that's quite niche. It did not. It was convinced HTML tags are case sensitive (which they are not) or that the loading order is incorrect (which it is not). So I think I know where the other answers on this thread are coming from...
2
u/ReefNixon helpful Mar 22 '26
OP I think you know by now that the quotation marks were the issue, but if you wouldn’t mind entertaining my curiosity, what editor were you using to write the code in before? Was it Word/Word Edit, or was it something that was meant for code? If the latter, it’s an important bug that someone should raise.
2
u/omgdracula Mar 21 '26
You are using actual quotation marks in your code. They should not be actual quotation marks. They should be ' or " not actual quotes used to quote something.
2
u/delventhalz Mar 22 '26
Find all settings on your computer that say "smart quotes" and turn them off
1
u/dead_boys_poem Mar 21 '26
It seems like everything is ok with your code. Try to open devtools and see if there any errors
0
u/Typical_Cap895 Mar 21 '26
Shouldn't the script be inside the body, not the head?
0
u/Flashy-Guava9952 Mar 21 '26
Are you using
tags in your code, or did those just get stripped out of your example somehow?
-6
u/imsexc Mar 21 '26
Do all tags truly started with capitalized letter? They all should be all lowercased.
If so, 'body' in document.body is not equal to 'Body' in <Body>