0

Background color randomizer

Why isn't this working? https://code.sololearn.com/W45K5UiBVb14/#js

20th Nov 2018, 6:25 PM
Yosharu
Yosharu - avatar
9 ответов
+ 2
You can't name function as bgColor. https://code.sololearn.com/WSGd49FaKxEm/?ref=app One other thing to note: You shouldn't use onclick. always use addEventlistener() https://www.sololearn.com/learn/JavaScript/2758/ edit: NIk01 I have changed everything. works fine
20th Nov 2018, 8:02 PM
Roneel
Roneel - avatar
+ 2
See schindlabua's comment in second page of lesson "Please, never use "<... onclick='...'>" or "x.onclick = ...". The proper way to do this (since the year 2000!) is var x = document.getElementById('demo'); x.addEventListener('click', function(){ document.body.innerHTML = Date(); }); as mentioned later in the tutorial."
20th Nov 2018, 8:12 PM
Roneel
Roneel - avatar
+ 2
I found this one. And read many answers. https://stackoverflow.com/questions/6348494/addeventlistener-vs-onclick Pros and cons both. There isn't a problem in such small projects.
20th Nov 2018, 8:36 PM
Roneel
Roneel - avatar
+ 2
Roneel thank you very much for the link. I’ve just learned how to attach more then 1 function to the same event :D. But I still will use onclick, when I need just one event handler )))
20th Nov 2018, 8:45 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 1
Jack Rehfeldt your array consists of just 1 string :D I saw it just now, you should write like this: [“red”, “blue”, “green”, etc...
20th Nov 2018, 6:31 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 1
Roneel why ? whats the difference between writing onclick in html and adding an event in js ?
20th Nov 2018, 8:10 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 1
I’ve read the comment and all the replies and still can’t get it. He didn’t say the reason he just says don’t use it, because don’t use it...
20th Nov 2018, 8:21 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 1
Jack Rehfeldt Did your problem solve ?
20th Nov 2018, 8:47 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 1
NIk01 Roneel Thank you both for your help!
22nd Nov 2018, 4:36 AM
Yosharu
Yosharu - avatar