More than one event | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

More than one event

I want to add more than one css background to js script (document.getElementById('back'). style.backgroundColor='black'") But now I want to change it back to white or none

28th Mar 2020, 2:23 AM
Abhishek Singh
Abhishek Singh - avatar
4 Answers
28th Mar 2020, 3:13 AM
Gordon
Gordon - avatar
+ 2
I'm slightly unclear on exactly how you want this to perform. I'm answering under the following assumption: You've setup an event listener - the initial action when the event occurs is changing the background color to black - now you'd like the NEXT time the event occurs to change to white or none. The simplest way I could think of is to edit your event listener function with a simple boolean value that you change each time the function is called. Then place your code to change the background color inside an if/else statement to change depending on the value of that boolean variable. For example a variable called "isBlackBg" that is false to start. if(isBlackBg) { // change background color to white/other isBlackBg = false; } else { // change background color to black isBlackBg = true; } 1st Event) -- Change background color to black --set isBlackBg to true 2nd Event) -- Change background color to white(or other) -- set isBlackBg to false 3rd Event) -- Change background color to black --set isBlackBg to true As you can see it will continuously switch back and forth between false and true switching the color based on that value.
28th Mar 2020, 3:15 AM
Nicholas Ray
Nicholas Ray - avatar
2nd Nov 2020, 10:02 PM
JFMINEBOY João Felipe
JFMINEBOY João Felipe - avatar
- 1
Hello
12th Apr 2020, 7:41 PM
Alina. jakshylykova🇰🇬
Alina. jakshylykova🇰🇬 - avatar