jQuery Datepicker with text input that doesn't allow user input

Elliot Vargas picture Elliot Vargas · Sep 30, 2008 · Viewed 247.6k times · Source

How do I use the jQuery Datepicker with a textbox input:

$("#my_txtbox").datepicker({
  // options
});

that doesn't allow the user to input random text in the textbox. I want the Datepicker to pop up when the textbox gains focus or the user clicks on it, but I want the textbox to ignore any user input using the keyboard (copy & paste, or any other). I want to fill the textbox exclusively from the Datepicker calendar.

Is this possible?

jQuery 1.2.6
Datepicker 1.5.2

Answer

Adam Bellaire picture Adam Bellaire · Sep 30, 2008

You should be able to use the readonly attribute on the text input, and jQuery will still be able to edit its contents.

<input type='text' id='foo' readonly='true'>