Where did I go wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Where did I go wrong?

window.onload = function() { setInterval(showTime, 1000); changeBodyColor(); } function showTime() { var d = new Date(); var hours = d.getHours(); var minutes = d.getMinutes(); var seconds = d.getSeconds(); document.getElementById("time").innerHTML = hours + ":" + minutes + ":" + seconds; } function bbgcolo(){ var s = new Date(); var secs = s.getSeconds(); if(secs % 2 === 0){ document.body.style.backgroundColor.innerHTML = "white"; } else { document.body.style.backgroundColor.innerHTML = "black"; } } I want to change the background color of the page but script isn't working.. Problem solved!! Thanks Thanks Thanks if you had intention of helping me.. But for now, I am closing this case.. If you have better ideas, please share!! https://code.sololearn.com/WcIpzGt2vy6V/?ref=app visit this 👆👆👆

3rd Apr 2021, 4:56 AM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar
20 Answers
+ 8
This is wrong: document.body.style.backgroundColor.innerHTML = "white"; This is right: document.body.style.backgroundColor = "white"; Similar applies to setting "black" obviously. innerHTML is for HTML elements. It isn't for CSS properties.
3rd Apr 2021, 5:00 AM
Josh Greig
Josh Greig - avatar
+ 5
To fix everything, you could do this. I made minimal changes so you could learn from them: window.onload = function() { setInterval(showTime, 1000); } function showTime() { var d = new Date(); var hours = d.getHours(); var minutes = d.getMinutes(); var seconds = d.getSeconds(); document.getElementById("time").innerHTML = hours + ":" + minutes + ":" + seconds; bbgcolo(); } function bbgcolo() { var s = new Date(); var secs = s.getSeconds(); if(secs % 2 === 0){ document.body.style.backgroundColor = "white"; } else { document.body.style.backgroundColor = "black"; } }
3rd Apr 2021, 5:24 AM
Josh Greig
Josh Greig - avatar
4th Apr 2021, 11:39 AM
Arushi
Arushi - avatar
+ 4
Mohammad Mehedi Hasan provide correct url of images
3rd Apr 2021, 5:46 AM
SAN
SAN - avatar
+ 3
Mohammad Mehedi Hasan for increment in i So, i am declare 'i' outside the function
3rd Apr 2021, 6:46 AM
SAN
SAN - avatar
3rd Apr 2021, 5:30 AM
SAN
SAN - avatar
+ 2
Mohammad Mehedi Hasan share you code link
3rd Apr 2021, 5:48 AM
SAN
SAN - avatar
+ 2
Mohammad Mehedi Hasan i think you want images changed by according time
3rd Apr 2021, 5:58 AM
SAN
SAN - avatar
+ 1
window.onload = function() { setInterval(showTime, 1000); } function showTime() { var d = new Date(); var hours = d.getHours(); var minutes = d.getMinutes(); var seconds = d.getSeconds(); document.getElementById("time").innerHTML = hours + ":" + minutes + ":" + seconds; if(seconds % 2 == 0){ document.body.style.backgroundColor = "white"; } else { document.body.style.backgroundColor = "black"; } var images = ["images.jpeg", "images (1).jpeg", "images (2).jpeg", "images (3).jpeg", "images (4).jpeg"]; for(var i=0; i<images.length; i++) { document.getElementById("imges").src.innerHTML = images[i]; if(i > images.length) { i =0; } } } Why the images aren't appearing?
3rd Apr 2021, 5:44 AM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar
+ 1
urls are correct.. I had put one of them inside src tag in html file
3rd Apr 2021, 5:47 AM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar
3rd Apr 2021, 5:51 AM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar
+ 1
umm.. yeah
3rd Apr 2021, 6:02 AM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar
+ 1
can you please explain what it does?
3rd Apr 2021, 6:09 AM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar
+ 1
Exams why I need to declare the variable i .. outside of the function showTime
3rd Apr 2021, 6:36 AM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar
+ 1
Ayush Mishra, did you just copy/paste my answer? It is word for word exactly the same as what I typed.
4th Apr 2021, 4:44 PM
Josh Greig
Josh Greig - avatar
+ 1
This adjusts text colour to be the opposite of the background colour. I also adjusted the background to be black when the second is even like you asked. window.onload = function() { setInterval(showTime, 1000); } function showTime() { var d = new Date(); var hours = d.getHours(); var minutes = d.getMinutes(); var seconds = d.getSeconds(); document.getElementById("time").innerHTML = hours + ":" + minutes + ":" + seconds; bbgcolo(); } function bbgcolo() { var s = new Date(); var secs = s.getSeconds(); if(secs % 2 === 1){ document.body.style.backgroundColor = "white"; document.body.style.color = "black"; } else { document.body.style.backgroundColor = "black"; document.body.style.color = "white"; } } Regarding the images problem, you should create a new question to tackle that after this background colour and text colour problem. It is harder to follow discussions about many diverging topics than stick to a single question. Also, share an example on Sololearn's Code Playground. https://code.sololearn.com/W00jB0xLzsh0/?ref=app is a broken link.
5th Apr 2021, 6:11 AM
Josh Greig
Josh Greig - avatar
+ 1
Problem solved!! Thanks Thanks Thanks if you had intention of helping me.. But for now, I am closing this case.. If you have better ideas, please share!! https://code.sololearn.com/WcIpzGt2vy6V/?ref=app visit this 👆👆👆
5th Apr 2021, 2:49 PM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar
0
You first need to make sure that the text colour of element #time can be visible over a black/white background colour window.onload = function() { setInterval( showTime, 1000 ); } function showTime() { var d = new Date(); document.getElementById("time").innerHTML = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); var bgColour = "white"; if( d.getSeconds() % 2 == 0 ) { bgColour = "black"; } document.body.style.backgroundColor = bgColour; }
3rd Apr 2021, 5:09 AM
Ipang
- 3
This is wrong: document.body.style.backgroundColor.innerHTML = "white"; This is right: document.body.style.backgroundColor = "white"; Similar applies to setting "black" obviously. innerHTML is for HTML elements. It isn't for CSS properties.
3rd Apr 2021, 3:54 PM
Ayush Kumar Mishra
Ayush Kumar Mishra - avatar