Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery remove the .change() handler

Tags:

html

jquery

forms

I have been trying to figure out how to remove the .change() jquery handler but have had no luck. I am using it to activate some form fields when a radio button is selected, but I don't want it to fire for every subsequent change.

$('.radio').change(function(){
    alert("changed");
    $('.fields').removeAttr('disabled');
    //this is where I try removing the change handler
    $(this).off('change');
});


JSfiddle demo

like image 625
JRulle Avatar asked Dec 20 '14 14:12

JRulle


2 Answers

change

// 'this' only removing the handler from the radio button that was changed
$(this).off('change');

to

// removing the handler of all radio button
$('.radio').off('change');

your code

$('.radio').change(function(){
  alert("changed");
  $('.fields').removeAttr('disabled');
  $('.radio').off('change');
});

http://jsfiddle.net/q5jqqgnt/3/

like image 177
Simone Nigro Avatar answered Sep 30 '22 21:09

Simone Nigro


Your code actually works, but only problem is that you are turning off the "Change" event only for the Radio button that you are clicking. Try to use $(".radio").off('change'); instead of $(this).off('change');

like image 28
jjk_charles Avatar answered Sep 30 '22 21:09

jjk_charles