How to disable key input inside jqueryui datepicker

I am using jQueryUI date picker, and when the user clicks on the text field and presses the enter key, the current date is populated. I want to avoid this. I tried this:

$('#datepicker').on('keypress',  function(e){
    if (e.which == 13) {
        e.preventDefault();
        e.stopPropagation();
        return false; 
    }
});

Bad luck

here is the demo link https://jsfiddle.net/shalini456/zwjzo175/

+4
source share
6 answers

Try the following:

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

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

        return false;
    }
}
+2
source

Try the following:

$('#datepicker').keypress(function(e){

    if (e.keyCode == 10 || e.keyCode == 13) //10 is Line Feed and 13 is Carriage Return
        e.preventDefault();

  });
0
source

, ... "#datepicker" https://jsfiddle.net/shalini456/zwjzo175/4/

JQuery

$(document).ready(function(){
    $( "#datepicker" ).datepicker();

  $("#datepicker").on('click', function(){
   $("#datepicker").blur();
  });


});
0

Bhupesh Kushwaha,

$(function() {
    $("input").bind('keydown', function (e) {
    if (event.which == 13) {
    $(this).trigger(e); 
    return false; 
  } 
    }).datepicker(); 
  });

: https://jsfiddle.net/zwjzo175/27/

0

,

, , , , , "Charly H", datepicker.

, , .

:

$(function() {
    $("input").bind('keydown', function (e) {
        if (event.which == 13) {
            e.preventDefault();
            e.stopPropagation();
            $(this).trigger(e); 
            return false;
        } 
    }).datepicker(); 
});
0

, .

, , , , .

As mentioned in "Charly H", keydown binding must occur before calling datepicker.

Then in the call to the keydown handler e.stopImmediatePropagation(). For instance.

  $('#datepicker').bind('keydown',function(e){

    if (e.keyCode == 10 || e.keyCode == 13) //10 is Line Feed and 13 is Carriage Return
        e.stopImmediatePropagation();
  })
  .datepicker();

Fiddle: https://jsfiddle.net/8fyvh8wd/2/

0
source

All Articles