I wrote a script that, in theory, should clean the input when the checkbox:checked . And when checkbox:not(:checked) , then the input should display the value that was before the input was cleared. But my code is not working.

 $(function() { var input = $('#back_date'); var value = input.val(); $('#way-indent').change(function() { var clearence = this.checked ? '' : value; $(input).val(clearence); }); }); 

    1 answer 1

    Strange, but it works for me. I propose to write the previous value in the data-attribute.

     $(function() { var input = $('#back_date'); var value = input.val(); $('#way-indent').change(function() { if (this.checked) { input.data('value', input.val()); input.val(''); } else { input.val(input.data('value')); } }); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="back_date" type="text" value="Test value" /><br /> <label><input id="way-indent" type="checkbox" />Очистить</label>