Help, lol. What's wrong with this JS. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2
24th Aug 2017, 9:03 AM
James Barrow
James Barrow - avatar
16 Answers
24th Aug 2017, 12:00 PM
James Barrow
James Barrow - avatar
+ 6
window.onload = function(){ var button = document.getElementById("button"); var rainbow = ["red", "orange","yellow","green","blue","indigo ", "violet"]; function change(){ document.body.style.background = rainbow[Math.floor(7*Math.random())]; } button.addEventListener("click", change); } math>Math body.background? what is it? I only know body.style.background "change()" seriously? It is "change" bc "click" event will do "change()" when click Absolutely I can explain more correctly and truly of that. But I'm afraid You will require more confuse
24th Aug 2017, 9:41 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 5
It still returns an error in the console why?
24th Aug 2017, 10:29 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
oh then Your code wrong again should be document.body.style.background =document.getElementById('thanks').innerHTML = rainbow[Math.floor(7*Math.random())]; document.getElementById('thanks') for select element with include id "thanks"
24th Aug 2017, 10:44 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 4
Yay nice
24th Aug 2017, 12:09 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
Because your script is executed before your html is loaded. So it cant find the button... I suggest you to put your whole script inside window.onload = function () { //your code }
24th Aug 2017, 9:07 AM
clement
clement - avatar
+ 3
....Maybe na?
24th Aug 2017, 10:57 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 3
oh sorry My mistake vision :P tyvm.innerHTML=document.body.style.background=rainbow[7*Math.random()|0] no ".color"
24th Aug 2017, 11:03 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 3
add "tyvm.style.color=" in front of the previous answer
24th Aug 2017, 12:04 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 3
ah ha I just don't get what you want that's all
24th Aug 2017, 12:06 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 2
Unfortunately that isn't working. It's popping a math error. Saying math is undefined.
24th Aug 2017, 9:37 AM
James Barrow
James Barrow - avatar
+ 2
thanks man. i think i got now. thanks for the critique of my code. 👍
24th Aug 2017, 9:47 AM
James Barrow
James Barrow - avatar
+ 2
i am still playing with the code. a Trying to get the color of the h1 tag to change also and that is what's throwing the error atm
24th Aug 2017, 10:34 AM
James Barrow
James Barrow - avatar
+ 2
so there is no need to use the variable tyvm?
24th Aug 2017, 10:58 AM
James Barrow
James Barrow - avatar
+ 2
Hey man, that fix changes what the h1 says. I want to change the color of the text.
24th Aug 2017, 11:58 AM
James Barrow
James Barrow - avatar
+ 2
check it, it works.
24th Aug 2017, 12:05 PM
James Barrow
James Barrow - avatar