How do I not allow typing in a text input field using jQuery?

20,139

Solution 1

$('#rush_needed_by_english').keydown(function() {
  //code to not allow any changes to be made to input field
  return false;
});

Solution 2

You could simply make the field read-only with:

$('#rush_needed_by_english').attr('readonly', 'readonly');

If you're using jQuery 1.6+, you should use the prop() method instead:

$('#rush_needed_by_english').prop('readOnly', true); 
// careful, the property name string 'readOnly' is case sensitive!

Or ditch jQuery and use plain JavaScript:

document.getElementById('rush_needed_by_english').readOnly = true;
Share:
20,139
zeckdude
Author by

zeckdude

Front and back-end Developer from Los Angeles, CA

Updated on July 09, 2022

Comments

  • zeckdude
    zeckdude almost 2 years

    I am using the jQuery datepicker on a text input field and I don't want to let the user change any of the text in the text input box with the keyboard.

    Here's my code:

    $('#rush_needed_by_english').keydown(function() {
      //code to not allow any changes to be made to input field
    });
    

    How do I not allow keyboard typing in a text input field using jQuery?