one way of doing it is by declaring a variable.
var inputValue;
$('#checkbox').change(function(){
if ($('#checkbox').is(':checked')){
inputValue = $('.test').val();
$('.test').val('');
} else {
$('.test').val(inputValue);
}
});
For multiple input fields. The idea of @Rashid is awesome. Store the value in the data-value attribute. And when the checkbox is unchecked you can get the values using data-value attribute.
$('#checkbox').change(function() {
if($('#checkbox').is(':checked')) {
$.each($('.test'),function(){
$(this).attr('data-value',$(this).val()).val("");
});
} else {
$.each($('.test'),function(){
$(this).val($(this).attr('data-value'));
});
}
});
$('.test').val('')from your code