When I make my datepicker read-only, I see that the user cannot type anything into the text box.
$(\"#datepicker\").attr(\'readonly\', \'readonly\');
Sorry, but Eduardos solution did not work for me. At the end, I realized that disabled datepicker is just a read-only textbox. So you should make it read only and destroy the datepicker. Field will be sent to server upon submit.
Here is a bit generalized code that takes multiple textboxes and makes them read only. It will strip off the datepickers as well.
fields.attr("readonly", "readonly");
fields.each(function(idx, fld) {
if($(fld).hasClass('hasDatepicker'))
{
$(fld).datepicker("destroy");
}
});
by making date picker input disabled achieve this but if you want to submit form data then its a problem.
so after lot of juggling this seems to me a perfect solution
1.make your HTML input readonly on some condition.
<input class="form-control date-picker" size="16" data-date-format="dd/mm/yyyy"
th:autocomplete="off"
th:name="birthDate" th:id="birthDate"
type="text" placeholder="dd/mm/jjjj"
th:value="*{#dates.format(birthDate,'dd/MM/yyyy')}"
th:readonly="${client?.isDisableForAoicStatus()}"/>
2. in your js in ready function check for readonly attribute.
$(document).ready(function (e) {
if( $(".date-picker").attr('readonly') == 'readonly') {
$("#birthDate").removeClass('date-picker');
}
});
this will stop the calendar pop up invoking when you click on the readonly field.also this will not make any problem in submit data. But if you make the field disable this will not allow you to submit value.
You can set the range allowed to some invalid range so the user can't select any date:
$("#datepicker").datepicker({minDate:-1,maxDate:-2}).attr('readonly','readonly');
All of the listed solutions give a mediocre user experience or cause issues if you want to re-enable the datepicker with it's settings in-tact. I used a method similar to making a select read-only, which is to disable it but add a hidden field with the same name. Check it:
Usage:
$("#datepicker").readonlyDatepicker(true); //makes the datepicker readonly
$("#datepicker").readonlyDatepicker(false); //makes the datepicker editable again
jQuery function:
$.fn.readonlyDatepicker = function (makeReadonly) {
$(this).each(function(){
//find corresponding hidden field
var name = $(this).attr('name');
var $hidden = $('input[name="' + name + '"][type="hidden"]');
//if it doesn't exist, create it
if ($hidden.length === 0){
$hidden = $('<input type="hidden" name="' + name + '"/>');
$hidden.insertAfter($(this));
}
if (makeReadonly){
$hidden.val($(this).val());
$(this).unbind('change.readonly');
$(this).attr('disabled', true);
}
else{
$(this).bind('change.readonly', function(){
$hidden.val($(this).val());
});
$(this).attr('disabled', false);
}
});
};
I set the following beforeShow event handler (in the options parameter) to achieve this functionality.
beforeShow: function(i) { if ($(i).attr('readonly')) { return false; } }
Readonly datepicker with example (jquery) -
In following example you can not open calendar popup.
Check following code see normal and readonly datepicker.
Html Code-
<!doctype html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>jQuery UI Datepicker functionality</title>
<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
rel = "stylesheet">
<script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
<script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<!-- Javascript -->
<script>
$(function() {
var currentDate=new Date();
$( "#datepicker-12" ).datepicker({
setDate:currentDate,
beforeShow: function(i) {
if ($(i).attr('readonly')) { return false; }
}
});
$( "#datepicker-12" ).datepicker("setDate", currentDate);
$("#datepicker-13").datepicker();
$( "#datepicker-13" ).datepicker("setDate", currentDate);
});
</script>
</head>
<body>
<!-- HTML -->
<p>Readonly DatePicker: <input type = "text" id = "datepicker-12" readonly="readonly"></p>
<p>Normal DatePicker: <input type = "text" id = "datepicker-13"></p>
</body>
</html>