Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 240
zeckdude Avatar asked Mar 10 '10 00:03

zeckdude


People also ask

How do you prevent a user from typing in an input field?

The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable.

How do you make a text field non editable in jQuery?

To make a textarea and input type read only, use the attr() method .

How do I stop people from typing in a Datepicker textbox?

You can use the below code to disable the editing of in KendoDatePicker. $("#datepicker"). attr("disabled","disabled"); In above code datepicker is the id of textbox control.


2 Answers

$('#rush_needed_by_english').keydown(function() {
  //code to not allow any changes to be made to input field
  return false;
});
like image 144
PetersenDidIt Avatar answered Sep 23 '22 17:09

PetersenDidIt


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;
like image 26
Cᴏʀʏ Avatar answered Sep 26 '22 17:09

Cᴏʀʏ