I am using jQueryUI date picker and when user clicks on a textbox and hits the enter key the current date gets populated. I want to avoid that. I have tried this:
$('#datepicker').on('keypress', function(e){
if (e.which == 13) {
e.preventDefault();
e.stopPropagation();
return false;
}
});
With no luck
here is the link of demo https://jsfiddle.net/shalini456/zwjzo175/
You can restrict the users from selecting a date within the particular range by specifying MinDate and MaxDate properties. The default value of MinDate property is 1/1/1920 and MaxDate property is 12/31/2120 .
Datepicker has a built-in disable method for this you can use, which also disables the text input, like this: $("#datepicker"). datepicker("disable");
To disable a datepicker in jQuery UI we will be using disable() method which is discussed below: jQuery UI disable() method is used to disable the datepicker. Parameters: This method does not accept any parameters.
$("#datepicker"). attr('readonly', 'readonly');
Try this:
$("#datepicker").keydown(myfunction); // use keydown
function myfunction(e) {
if(e.keyCode === 13) {
e.stopPropagation();
e.preventDefault();
return false;
}
}
Some of the solutions here appear to work because they throw an exception of some kind.
There are two things which seem to make it work without throwing an exception.
As mentioned by "Charly H" the keydown binding must happen before calling datepicker.
Then in the keydown handler call e.stopImmediatePropagation()
. E.g.
$('#datepicker').bind('keydown',function(e){
if (e.which == 13)
e.stopImmediatePropagation();
})
.datepicker();
Fiddle: https://jsfiddle.net/8fyvh8wd/18/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With