Once I change element’s class using jQuery, the click function does not work on the new class.
Here is my code
jQuery('.close').on('click', function() {
jQuery('div').slideUp();
jQuery(this).addClass('open');
jQuery(this).removeClass('close');
});
jQuery('.open').on('click', function() {
jQuery('div').slideDown();
jQuery(this).addClass('close');
jQuery(this).removeClass('open');
});
You can try it here – http://jsfiddle.net/NkG9k/1/
Try clicking on the toggle button again. (it won’t slidedown the div)
Try this, Apply
onon document and use class filter to attach the event to your button.Live Demo
You need to delegate event on static parent of the element you want this dynamic behavior so that when the class is changed (from
opentocloseand fromclosetoopen) of an element the event is bind automatically on this element according to newly changed class.Understanding Event Delegation