[fixed]Why background image of body can't be changed ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

19th Sep 2020, 7:31 AM
Prasant
Prasant - avatar
5 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
19th Sep 2020, 8:24 AM
Rei
Rei - avatar
+ 7
Prasant replace your line 24 with either of this var theImg = document.getElementsByTagName("BODY"); OR var theImg = document.querySelector("body");
19th Sep 2020, 7:43 AM
Rohit
+ 4
Thank you RKK and Rei 😊 I got my mistake now
19th Sep 2020, 8:53 AM
Prasant
Prasant - avatar
+ 3
Kode Krasher Thank you 😊
19th Sep 2020, 8:54 AM
Prasant
Prasant - avatar
+ 3
Can I use querySelector everywhere without using getElementById / ByClassName / TagName / others ? I don't know about query selectors 😔
19th Sep 2020, 9:07 AM
Prasant
Prasant - avatar