tags:

views:

479

answers:

5

I have a HTML div element on a page that is updated when a select element on the page changes. Sometimes the input tags (text boxes) need to be disabled based on the selection.

Here is the problem: the project stakeholders like the functionality, they just think in the "disabled" state, the text box contents are too light and therefore unreadable. Can I apply CSS to a disabled control? Or should I be setting the text boxes to readonly and using some other CSS. This application is also using jQuery, if that helps. And ideas or suggestions here?

+2  A: 

Applying CSS to an element is not affected by whether the element is disabled.

Upper Stage
except text color in IE
fudgey
+1  A: 

I would set them to readonly, as apparently you're wanting people to be able to read them, people may also want to select text from them for copy/paste, and selection is disabled as well when the text box is disabled. However, your CSS should work fine whether or not the element is disabled.

+1 for mentioning readonly controls.
R. Bemrose
+1  A: 

Yes, you can. In CSS:

input[disabled]{color:green}

Or jQuery:

$('input[disabled]').css('color','green');

UPDATE: This only works in gecko/webkit.

David
+1. I had an `<input type="image" ...>` which wasn't styled correctly when the disabled attribute was set/reset with jQuery. `input[disabled='disabled']` (css) didn't work in this scenario. `input[disabled]` did.
Lette
A: 

If you have trouble with a non-overwritable style in a disabled element - Upper Stage points out that there is no limit on styling, and on second thought he may be right - you may be best off with giving the controls a readonly attribute and "manually disabling" it using onfocus='this.blur();'

Note that this will not work 100% like a disabled control: Its value will be submitted with the form.

Pekka
+1  A: 
input[type="submit"][disabled="disabled"]
{
    /* CSS here */
}
input[type="text"][disabled="disabled"]
{
    /* CSS here */
}
input[type="button"][disabled="disabled"]
{
    /* CSS here */
}

Works in every browser if the input tag is something like:

<input type="submit" disabled="disabled">

and if you want to change it when it is not disabled, just add this to your CSS

input[type="submit"]
{
    /* CSS here */
}
input[type="text"]
{
    /* CSS here */
}
input[type="button"]
{
    /* CSS here */
}
Carlos