Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable enter key inside jqueryui datepicker

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/

like image 912
Shalini Avatar asked Mar 04 '16 19:03

Shalini


People also ask

How do I restrict datepicker?

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 .

How do I disable UI datepicker trigger?

Datepicker has a built-in disable method for this you can use, which also disables the text input, like this: $("#datepicker"). datepicker("disable");

How do I disable datepicker?

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.

How do I make a datepicker read only?

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


2 Answers

Try this:

$("#datepicker").keydown(myfunction); // use keydown

function myfunction(e) {
    if(e.keyCode === 13) {
        e.stopPropagation();
        e.preventDefault();

        return false;
    }
}
like image 80
Bhupesh Avatar answered Sep 22 '22 16:09

Bhupesh


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/

like image 32
OlduwanSteve Avatar answered Sep 23 '22 16:09

OlduwanSteve