Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable backspace returning to previous page in readonly textbox mvc4 razor

I have a datepicker field set to read only. I don't want the user to enter dates, but instead only select them from the calendar. This works fine. However, the moment I hit backspace in this readonly field, it redirects to the previous page. I would like to stop this functionality.

I have already included a "clear" button on the calendar for clearing purposes. However, when I still select all (the date) in this readonly field and try to clear (viz backspace) it does the above. Any assistance would be appreicated!!

like image 847
New2This Avatar asked Dec 18 '25 19:12

New2This


1 Answers

The following appears to work in my tests, and you'll only prevent backspace from going back in the browser when in the actual input this way, instead of breaking the shortcut globally for the whole page.

$('input[readonly]').on('keydown', function (e) {
    if (e.which === 8) {
        e.preventDefault();
    }
});
like image 86
Chris Pratt Avatar answered Dec 21 '25 23:12

Chris Pratt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!