I want to backup a textbox value in order to allow table filtering. When user press backspace and leave the textbox empty I want to put the previous value I backed up previously:
vbackup stores the initial textbox value, this is "hello" in the jsfiddle.
$('#btn').click(function () {
$('#mydiv').slideToggle('fast', function () {
// I SAVE THE TEXTBOX VALUE SO THAT I CAN
// GET IT BACK AFTERWARDS
var vbackup = $('#txtbox').val();
alert(vbackup);
if ($(this).is(":visible")) {
// NO MATTERS...
}
else {
// IF TEXTBOX VALUE IS EMPTY I GET THE PREVIOUS
// VALUE BACK
if ($('#txtbox').val() == '') {
// VBACKUP IS EMPTY !!
alert(vbackup );
$('#txtbox').val(vbackup );
}
}
});
return false;
});
I can't understand why my vbackup variable matches with the textbox actual value since I backuped up earlier.
Procedure:
- Press the button once
- empty the textbox
- Press the button again. Now the "hello" word should be get back from the variable to the textbox. The alert tells that the value in my variable has been overriden.