3

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!!

braX
  • 11,506
  • 5
  • 20
  • 33
New2This
  • 253
  • 1
  • 6
  • 22
  • 2
    check if [this](http://stackoverflow.com/questions/11112127/prevent-backspace-from-navigating-back-with-jquery-like-googles-homepage) helps – tweray Oct 20 '14 at 14:37
  • @tweray: Technically, that won't work because it explicitly excludes inputs and textareas so that you can still do backspaces there. Here, the OP's problem is with backspacing *within* this readonly input. – Chris Pratt Oct 20 '14 at 15:38

1 Answers1

3

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();
    }
});
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444