javascript - jQuery Problems with removing event handler with .off() -
i have tried remove event handler .off()
, not work.
i want remove event handlers 1 input:
<input type="file" id="file_upload" name="file_upload" size="50" />
here have tried:
$('#form').on('keyup change', ':input', function() { registerchange(); }); $('#form input[name=file_upload]').off(); $('#file_upload').off();
you can use not
selector file upload button
$('#form').on('keyup change', 'input:not([name=file_upload])', function() { registerchange($(this).val()); }); function registerchange(value) { // notice typing upload textbox not log console console.log(value); } $('#form').on('click', 'input[name=file_upload]', function () { alert('in click event handler, removing myself'); // here how can remove event handler $('#form').off('click', 'input[name=file_upload]'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form id="form"> <input type="text" /> <input type="text" /> <!-- changeing textbox demo --> <input type="text" id="file_upload" name="file_upload" size="50" value="choose file" /> </form>
open console window (ctrl+shift+j in chrome) , type of text boxes. notice how first 2 text boxes write console. note last text box defines on click handler removes it, why alert shows once.
Comments
Post a Comment