user3351236
user3351236

Reputation: 2538

JQuery function still working after removeing the class

I add and remove classes dynamicaly. But when I remove class the event still working. How to stop event to work.

$('.input').on('paste keypress, keydown', function(event) {
         fff();
}) 

But as soon as I add the class I need it to work.

Upvotes: 0

Views: 70

Answers (2)

Mayank
Mayank

Reputation: 1392

In case you can not add a static class to parent you can remove the assigned events after removing the class like below

FIDDLE,

javascript

function fff() {
  alert('chacha!!');
}
$(function() {
  $('#removeClass').on('click', function() {
    $('.input').removeClass('input').off('paste keypress, keydown');
  });
  $('.input').on('paste keypress, keydown', function(event) {
    fff();
  })
});

Hope it works for you

Upvotes: 1

Adil
Adil

Reputation: 148150

Use event delegation and bind event with static parent.

$('.static-parent-class').on('paste keypress, keydown', '.input',  function(event) {
    fff();
}); 

Upvotes: 2

Related Questions