Please why is this not changing colors | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3
17th Sep 2018, 3:08 AM
idris
idris - avatar
5 Answers
+ 6
window.onload = function(){ // window.onload so our javascript code loads after our html doc is loaded var colors=[ "purple", "orange", " yellow", "green", "blue", "red" ]; var squares =document.querySelectorAll(".square"); for(var i=0; i<squares.length; i++){ //alert(); // this was annoying don't do this ever!!! // squares[i].background=colors[i]; // you must access the backgroundColor from the style property of the element (squares[i]).style.backgroundColor= colors[i] } } // onload end #working example below https://code.sololearn.com/WT7RqfM47daM/?ref=app
17th Sep 2018, 4:06 AM
Lord Krishna
Lord Krishna - avatar
+ 3
https://code.sololearn.com/WdSxB6ctJ8RB/?ref=app
17th Sep 2018, 4:04 AM
Calviղ
Calviղ - avatar
+ 3
idris Instead of alert() try doing a console.log('txt') it's less intrusive. It's wasn't necessary to add parens in that context, did it as it was neat & more clear to me.
17th Sep 2018, 4:22 AM
Lord Krishna
Lord Krishna - avatar
+ 3
Lord Krishna ooh ok thank you very much
17th Sep 2018, 4:25 AM
idris
idris - avatar
+ 1
Lord Krishna the reason i put the alert in there was to check if the for loop was actually working i forgot to remove it. Also why was (squares[i]) but in a bracket i dont get that part please could you explain
17th Sep 2018, 4:16 AM
idris
idris - avatar