Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you make a date picker readonly?

To render the DatePicker into a read-only state, set its readonly property to true .

How do I create a date field readonly in jQuery?

$("#datepicker"). attr('readonly', 'readonly');

How do I restrict datepicker?

When Start Date is selected, then need to disable all the dates before selected start date + 4 days in end date datepicker. "minDate" option is our choice here. If end date is selected first, then disable all the dates after selected end date - 4 days in Start date datepicker. "maxDate" option is our choice here.

How do I make datepicker textbox editable?

1 Answer. Show activity on this post. $('#start_date'). datetimepicker({timeFormat: "HH:mm:ss", dateFormat:"dd-mm-yy", constrainInput: false, maxDate: maxdate});


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

Based on my experience I would recommend the solution suggested by Adhip Gupta:

$("#my_txtbox").attr( 'readOnly' , 'true' );

The following code won't let the user type new characters, but will allow them to delete characters:

$("#my_txtbox").keypress(function(event) {event.preventDefault();});

Additionally, this will render the form useless to those who have JavaScript disabled:

<input type="text" readonly="true" />

try

$("#my_txtbox").keypress(function(event) {event.preventDefault();});

If you are reusing the date-picker at multiple places then it would be apt to modify the textbox also via JavaScript by using something like:

$("#my_txtbox").attr( 'readOnly' , 'true' );

right after/before the place where you initialize your datepicker.