+ 3
Please why is this not changing colors
5 ответов
+ 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
+ 3
https://code.sololearn.com/WdSxB6ctJ8RB/?ref=app
+ 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.
+ 3
Lord Krishna ooh ok thank you very much
+ 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