+ 2
[fixed]Why background image of body can't be changed ?
I have seen in W3Schools how it changes the background image of body with onclick https://www.w3schools.com/JSREF/tryit.asp?filename=tryjsref_style_backgroundimage But when I tried it using if else statement with this code https://code.sololearn.com/Wk7z88x82wB6/?ref=app It didn't work. Why
8 Answers
+ 4
getElementsByTagName return array-like object.
so just get its first element
var theImg = document.getElementsByTagName("BODY")[0].style.backgroundImage;
personally i still prefer RKK's 2nd solution with querySelector
+ 7
Prasant replace your line 24 with either of this
var theImg = document.getElementsByTagName("BODY");
OR
var theImg = document.querySelector("body");
+ 3
Kode Krasher
Thank you đ
+ 3
Can I use querySelector everywhere without using getElementById / ByClassName / TagName / others ?
I don't know about query selectors đ