Create Toggle Button Using jQuery

user6571878

I want to use just ONE button to control Opening and Closing an Off-Canvas Menu. So I created a Button with OpenButton class which open menu, after clicking, I remove OpenButton class and add CloseButton class, These all work like a charm, But When I call Click Event on CloseButton It doesn't work, What is the problem ? This is my code :

$('.OpenButton').click(function() {
    $(this).addClass('CloseButton');
    $(this).removeClass('OpenButton');
});

$('.CloseButton').click(function() {
    alert('Close');
});
David R

Since you're adding the class dynamically, you need to use event delegation to register the same with the event handler mechanism,

$(document).on('click', ".CloseButton", function() {
    alert('Close');       
});

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related