javascript - Something wrong with my toggleclass -


there wrong jquery script. kinda works, when try toggle between classes works @ first. script changes classes it's supposed to. when try again, have click link , somewhere else, , works again. want able repeat whole procedure on , on without having click 2 times.

var clickonsettings = true; $(".settings_link").click(function () {     event.preventdefault();     if (clickonsettings) {         $("#coverup").toggleclass("cover1");         $("#settingani").toggleclass("settings1");         clickonsettings = false;     } }); $("#coverup").click(function () {     if (!clickonsettings) {         $("#coverup").toggleclass("cover2");         $("#settingani").toggleclass("settings2");         clickonsettings = true;     } }); 

created jsfiddle:

http://jsfiddle.net/5dzt1v6f/13/

if toggle 'cover1', toggle between having , not having 'cover1' on element. not imply automatically cover2 when remove cover1.

you have yourself. fortunately, toggleclass has second parameter accepts boolean. allows toggle classes on or off based on conveniently introduced boolean.

furthermore, makes click handler both elements same:

var clickonsettings = false; $( ".settings_link, #coverup" ).click(function() {      event.preventdefault();     clickonsettings = ! clickonsettings;     //toggle classes.     $( "#coverup" ).toggleclass( "cover1", clickonsettings);     $( "#settingani" ).toggleclass( "settings1", clickonsettings);     $( "#coverup" ).toggleclass( "cover2", !clickonsettings);     $( "#settingani" ).toggleclass( "settings2", !clickonsettings); }); 

http://jsfiddle.net/5dzt1v6f/20/


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -