0

I've written the following simple jQuery to show a submenu. When I click the link to open the menu, the menu opens and animates properly, and adds the new class to the links. However, when I click the newly classed link to close the submenu, nothing happens. This makes no sense to me, can anyone tell me what I'm doing wrong?

Thank you very much for any assistance, here is my code:

// DOM ready
$(document).ready(function () {

    // Options menu functionality
    $('a.menu-closed').click(function () {
        $('.left-menu a').removeClass('menu-closed');
        $('.left-menu a').addClass('menu-open');
        $('.options').animate({width: '20%'}, 200);
        $('.main-content').animate({width: '60%'}, 200);
    });
    $('a.menu-open').click(function () {
        $('.left-menu a').removeClass('menu-open');
        $('.left-menu a').addClass('menu-closed');
        $('.options').animate({width: '0%'}, 200);
        $('.main-content').animate({width: '80%'}, 200);
    });

});// /DOM ready

1 Answer 1

1

The DOM is updated. You need to use a "persistent" event listener, like :

$(document).on('click', 'a.menu-closed', function () {
  $('.left-menu a').removeClass('menu-closed');
  $('.left-menu a').addClass('menu-open');
  $('.options').animate({width: '20%'}, 200);
  $('.main-content').animate({width: '60%'}, 200);
});

$(document).on('click', 'a.menu-open', function () {
  $('.left-menu a').removeClass('menu-open');
  $('.left-menu a').addClass('menu-closed');
  $('.options').animate({width: '0%'}, 200);
  $('.main-content').animate({width: '80%'}, 200);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.