javascript - How to unbind a jquery plugin in IE8 -
i have jquery plugin in code force input box accept numeric input @ point have disable , make accept alphabets also. cannot use unbind() because unbind other features well. appreciated, thanks.
here jsfiddle https://jsfiddle.net/mpr73v9w/ , below code.
<input type="text"> <button>button</button>  $.fn.forcenumericonly = function () {     return this.each(function () {         $(this).keydown(function (e) {             var key = e.charcode || e.keycode || 0;              if (e.shiftkey) {                 e.preventdefault();             }              // allow backspace, tab, delete, arrows, numbers , keypad numbers             return (                 key == 8 ||                 key == 9 ||                 key == 13 ||                 key == 46 ||                 (key >= 37 && key <= 40) ||                 (key >= 48 && key <= 57) ||                 (key >= 96 && key <= 105));         });     }); };  $("input").forcenumericonly();  $("button").click(function() {     $('input').forcenumericonly.destroy(); });      
instead of using named event method $.keydown() can use $.on() method allows event namespaces.
$(this).on('keydown.forcenumericonly', function(e) {      // handle event });   this makes easy unbind specific events without interfaring other features. .off() method.
$.fn.forcenumericonly.destroy = function () {     $(this).off('.forcenumericonly'); };   you can unbind keydown event $(this).off('keydown.forcenumericonly') or in example unbind events in forcenumericonly namespace $(this).off('.forcenumericonly').
Comments
Post a Comment