Arthik Arthik - 13 days ago 5
Javascript Question

The value not changed in the keydown event

I have the following function:

formCreated = function (event, data) {
console.log(data.formType); // output: 'edit'

$('body').on('keydown', function (e) {
if (e.ctrlKey && e.which == 80) {
e.preventDefault();
e.stopPropagation();

if (data.formType == 'create') // not changed until page refresh
alert('save the record');
else if (data.formType == 'edit')
_connectPrinter(data);
}
});
}


After saving record the
data.formType
changed as 'edit', but inside the event value not changed until page refresh.

Answer

Try http://api.jquery.com/off/,

$('body').off('keydown').on('keydown', function (e) {
   // your code here
}

The .off() method removes registered values.