Myles O'Connor
Myles O'Connor

Reputation: 15

jQuery class menu not displaying properly

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

Upvotes: 0

Views: 47

Answers (1)

Paul Rad
Paul Rad

Reputation: 4882

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);
});

Upvotes: 1

Related Questions