I am (very) new to jQuery so please be gentle but I cannot seem to get the basic script below to work properly.
The immediate class when loaded is .menuFlyout and when it’s clicked it changes as needed however the second part to change it back does not work. noConflict() doesn’t help.
Thanks in advance for any help.
jQuery(document).ready(function() {
jQuery('.menuFlyout').click(function(){
jQuery('.menuFlyout').addClass('closeFlyout');
jQuery('.menuFlyout').removeClass('menuFlyout');
});
jQuery('.closeFlyout').click(function(){
jQuery('.closeFlyout').addClass('menuFlyout');
jQuery('.closeFlyout').removeClass('closeFlyout');
});
});
Try with this
The problem with your code is that there is no element with class
closeFlyoutwhen you attah the handler. You need to delegate the click handler using on method (see section “Direct and delegated events” for more details ).Using event delegation should fix your problem but as a side note here you have 2 improved versions of your code (as sugggested by @wirey and @h0tw1r3)
or