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:
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); });
Comments
Post a Comment