+ 1

I can't get the .style method to work...

I was doing a small project with 3 divs that act as buttons. The buttons will change color when pressed, but there should only be one button that is colored at a time. I was only making the first button and I found out that it doesnt work. https://code.sololearn.com/WGvKfJsnrAo4/?ref=app https://code.sololearn.com/WGvKfJsnrAo4/?ref=app

25th Dec 2017, 4:02 AM
Haziq Hairil
Haziq Hairil - avatar
3 Réponses
+ 14
Replace the JS code with this :- function colorChange() { var item1 = document.getElementById("item1").style.backgroundColor = "blue"; } or Add the JS code in a script tag just before closing the <body> tag. This is because your script is loading before the CSS part ...
25th Dec 2017, 4:24 AM
Nikhil
Nikhil - avatar
+ 12
One more possible way is ( this I use mostly) :- Put all your code in the JS tab in a window.onload function Example :- window.onload = function (){ //Your JS code here }
25th Dec 2017, 8:44 AM
Nikhil
Nikhil - avatar
+ 1
Thanks Nikhil!
25th Dec 2017, 4:27 AM
Haziq Hairil
Haziq Hairil - avatar