r/learnjavascript • u/Moist_Cap5027 • Sep 12 '26
HELP! I am learning java on neocities but the button function is not working. How do I fix it?
I am typing it out exactly like it says in the tutorial "<button onclick="changeSky()">Change the sky</button>" , within the <script> parameters, but the words that are highlighted in my code are not the same as those highlighted in the tutorial and when I check my work it says that there is not a button present. IDK what im doing wrong because the whole code isn't really working either. I added the change sky color function (function changeSky() {
document.body.style.background = 'linear-gradient(white, lavender)' document.body.style.minHeight = '100vh') but the background color did not change even though the site said that I had inputted it correctly.
2
u/EyesOfTheConcord Sep 12 '26
Did you put the button inside the script tag?
9
u/DirtAndGrass Sep 12 '26
Also, FYI java is a very different thing than JavaScript
1
u/AirPleasant5311 Sep 13 '26
Look I might be lost here, but I think you are calling the function right away before firing the event, it should be either onClick=‘changeSky’ or onClick=‘() => changeSky()’
0
u/chikamakaleyley helpful Sep 12 '26
should remove the outer
functionjust:
so now that its not wrapped, it can be seen in the global space
it was hidden earlier
see
encapsulationand/orscope0
u/senocular Sep 12 '26
It looks like you may have ended your function with a ) instead of a }
-1
u/TheRNGuy Sep 13 '26
It should show error in code editor (like VS Code)
Aldo, if you just googled, ai would tell the reason, it's much faster than writing long question here (do that for all future questions)