jquery toggle() problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

jquery toggle() problem

I wanted to write a code that after toggling(hiding, showing) the div element checks for the existence of it so that if exists, makes the background color of body element red and if not makes it blue. it makes it red even when I check for non-existence of it! and it doesn't make it blue after becoming hidden how should rewrite the code to fix it? https://code.sololearn.com/W6aw2moZy6u0/?ref=app

1st Jul 2021, 9:56 PM
salar vahidi
salar vahidi - avatar
3 Answers
1st Jul 2021, 10:30 PM
Arturop
Arturop - avatar
+ 1
Arturop thank you. that worked I also found another way to approach this: https://code.sololearn.com/W2tUnsCTX21E/?ref=app
1st Jul 2021, 11:34 PM
salar vahidi
salar vahidi - avatar
0
$(function() { let f = true; $("p").click(function() { $("div").toggle(1000, function(){ if(f){ $("body").css("background-color", "red"); f = false; } else{ $("body").css("background-color", "blue"); f = true; } }); }); });
1st Jul 2021, 10:50 PM
Solo
Solo - avatar