Jeysingh Anbu Jeysingh Anbu - 6 months ago 185
Javascript Question

How to prevent only closing of bootstrap modal on backspace keypress

I have a Form in bootstrap Modal. I have disabled the backspace button when bootstrap modal is open by this code

$('body').keydown(function (e) {
if ($('#myModal').is(':visible')) {
if (e.keyCode == 8) {
return false;
}
}
});


In bootstrap modal form I have textbox, textarea I need backspace button there.
I am not able to use the backspace button in textbox which is in the bootstrap modal form

Answer

Solution:

$('body').keydown(function (e) {
    if ($('#myModal').is(':visible')) {
        var rx = /INPUT|SELECT|TEXTAREA/i;
        if (e.keyCode == 8) {
            if(!rx.test(e.target.tagName) || e.target.disabled || e.target.readOnly ){
                e.preventDefault();
            }
        }
    }
});
Comments