I want to make html code on button click change background color simultaneously | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I want to make html code on button click change background color simultaneously

My friend has made a project that if he clicks on button the background image or background color changes on button click. If he click for first time then the color would be sky color on second click it will become darker and on third click it will become black. Can any body help me please.

2nd Aug 2022, 6:06 AM
ARYAN Khuman
ARYAN Khuman - avatar
5 Answers
+ 3
Firstly create an array of colors. In javascript var colors = ["skyblue","blue","black"] And then the button in the html <button onclick="change()">Change</button> Then in javascript, Define the change function var I=0 function change(){ I++ document.body.style.backgroundColor=colors[I] } See this code https://code.sololearn.com/WeayjyJNvynY/?ref=app
2nd Aug 2022, 8:45 AM
Chigozie Anyaeji 🇳🇬
Chigozie Anyaeji 🇳🇬 - avatar
+ 3
I++ if (I == colors.length) I=0 // reset
2nd Aug 2022, 10:24 AM
SoloProg
SoloProg - avatar
+ 1
ARYAN Khuman you welcome.
2nd Aug 2022, 1:07 PM
Chigozie Anyaeji 🇳🇬
Chigozie Anyaeji 🇳🇬 - avatar
0
I want to make a website in which one rockets is in between of the page, 2 buttons up and down, if I click up button background will change Fromm one to 2nd if I click again up button bg changes again to 3rd and if I click down button bg changes to 2nd and if I click down button again bg changes to 1
2nd Aug 2022, 6:38 AM
ARYAN Khuman
ARYAN Khuman - avatar
0
Thanks bro
2nd Aug 2022, 9:46 AM
ARYAN Khuman
ARYAN Khuman - avatar