Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable text box without style changing?

Tags:

html

jquery

css

How can you disable an HTML text box without having the browser change the visual appearance of the textbox?

We disable with JQuery: $(".datepick").attr('readonly', 'readonly');

Works fine but in Firefox the text box appears with a grey background and a thick border. We want to prevent this from happening.

Reason we are doing this is to make a JQuery date picker text box read only (so the user has to use the calendar popup area).

like image 553
Marcus Leon Avatar asked Nov 11 '10 20:11

Marcus Leon


People also ask

How do I disable a text box in CSS?

You can't disable a textbox in CSS. Disabling it is not a presentational task, you will have to do this in the HTML markup using the disabled attribute.

How do I get rid of text box without Greying out?

You could set the Enabled property of the text box to True, and its Locked property to True also. That way, the text will not be grayed out.

How do you prevent user from typing in text field without disabling the field?

To prevent user from typing in text field without disabling the field with HTML, we can add the readonly attribute to the input. to stop users from enter text into the input without making it look disabled.


2 Answers

You could do it by blurring the text input on focus:

$('.datepick').focus(function(){
    this.blur();
});

Alternatively, you could just disable text entry in the input:

$('.datepick').keydown(function(e){
    e.preventDefault();
});
like image 121
lonesomeday Avatar answered Sep 20 '22 21:09

lonesomeday


If you want to override the styling for a disabled input box, explicitly set the styling for an input text box like so:

input[type=text] {
  color: #000;
  background-color: #fff;
}
/* alternatively you could add textarea to this as well */

additionally you could set a css class like 'disabled' whenever you set the input to disabled and use a disabled css style like so:

input[type=text].disabled {
  /* disabled styling goes here */
}

and then have some code like this:

$(some_input).attr('disabled', 'disabled');
$(some_input).addClass('disabled');

and to reenable the field you could try something like:

$(some_input).removeAttr('disabled');
$(some_input).removeClass('disabled');
like image 21
aarona Avatar answered Sep 20 '22 21:09

aarona