I want to remove datepicker function depending on the dropdownlist selected value. I try the following codes, but it still shows the calendar when I put the cursor in the t
Just bind the datepicker to a class rather than binding it to the id . Remove the class when you want to revoke the datepicker...
$("#ddlSearchType").change(function () {
if ($(this).val() == "Required Date" || $(this).val() == "Submitted Date") {
$("#txtSearch").addClass("mydate");
$(".mydate").datepicker()
} else {
$("#txtSearch").removeClass("mydate");
}
});
Destroy the datepicker's instance when you don't want it and create new instance whenever necessary.
I know this is ugly but only this seems to be working...
Check this out
$("#ddlSearchType").change(function () {
if ($(this).val() == "Required Date" || $(this).val() == "Submitted Date") {
$("#txtSearch").datepicker();
}
else {
$("#txtSearch").datepicker("destroy");
}
});
Well I had the same issue and tried "destroy" but that not worked for me. Then I found following work around My HTML was:
<input placeholder="Select Date" id="MyControlId" class="form-control datepicker" type="text" />
Jquery That work for me:
$('#MyControlId').data('datepicker').remove();
what about using the official API?
According to the API doc:
DESTROY: Removes the datepicker functionality completely. This will return the element back to its pre-init state.
Use:
$("#txtSearch").datepicker("destroy");
to restore the input to its normal behaviour and
$("#txtSearch").datepicker(/*options*/);
again to show the datapicker again.
You can try the enable/disable methods instead of using the option method:
$("#txtSearch").datepicker("enable");
$("#txtSearch").datepicker("disable");
This disables the entire textbox. So may be you can use datepicker.destroy() instead:
$(document).ready(function() {
$("#ddlSearchType").change(function() {
if ($(this).val() == "Required Date" || $(this).val() == "Submitted Date") {
$("#txtSearch").datepicker();
}
else {
$("#txtSearch").datepicker("destroy");
}
}).change();
});
Demo here.
This is the solution I use. It has more lines but it will only create the datepicker once.
$('#txtSearch').datepicker({
constrainInput:false,
beforeShow: function(){
var t = $('#ddlSearchType').val();
if( ['Required Date', 'Submitted Date'].indexOf(t) ) {
$('#txtSearch').prop('readonly', false);
return false;
}
else $('#txtSearch').prop('readonly', true);
}
});
The datepicker will not show unless the value of ddlSearchType is either "Required Date" or "Submitted Date"