34

I noticed that you can change the colour of text in textboxes which are disabled in Firefox applying a simple class but could not get a way to do it in IE 6/7. Does anyone out there have a elegant solution to achieve this.

chugh97
  • 9,602
  • 25
  • 89
  • 136
  • 2
    I am not using jQuery and don't know how to use it...I am looking for a elegant solution in CSS – chugh97 Mar 02 '09 at 12:24

11 Answers11

77

I noticed that you can change the colour of text in textboxes which are disabled in Firefox

I think what the question is trying to say is that this:

<textarea disabled="disabled" style="color: red;">Hello</textarea>

Results in grey text in IE, vs. red in Fox. FWIW, Opera also gives grey, whilst the WebKit browsers give red.

This is a pure CSS issue to do with how much form fields are rendered according to the OS's widget set and how much according to the CSS rules. This has always been an area of great cross-browser difference. Scripting is not relevant, much though SO would like “use jQuery” to be the answer to every question.

The usual workaround is to use ‘readonly’ instead of ‘disabled’, then use styling (eg. based off ‘class="disabled"’) to reproduce whatever shaded disabled effect you want. ‘readonly’ controls are not turned into OS-level-disabled widgets, giving you more latitude to style them.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • Thanks for the explanation...but I have a futher question...How do I make other controls like CheckboxLists etc on the form ReadOnly as they dont seem to have this property on them. – chugh97 Mar 02 '09 at 13:56
  • 3
    Yes, readonly is only available for – bobince Mar 02 '09 at 14:57
  • 8
    I am a little perplexed as to why this wasn't the selected answer. Thanks. – Carvell Fenton Nov 03 '11 at 17:52
  • 1
    The problem with changing inputs to readonly is that their data gets sent to the server when you submit the form. This is not the behaviour of a disabled input, and usually means you have to write code on the server side to anticipate data from the "disabled" field. Otherwise you have potential issues where people can send data to the server that they shouldn't be allowed to. – BadHorsie Sep 11 '13 at 10:42
  • @BadHorsie: the usual approach is not to give the input a `name`, so it doesn't form part of the submission data. This does mean that any JavaScript code has to be using IDs to get hold of inputs rather than old-school name-based `form.elements` collection, but this is best practice anyhow. – bobince Sep 11 '13 at 14:25
27

     It should be noted that using the disabled attribute causes the underlying <input> element not to be submitted during a form submit, where as readonly controls are submitted to the server. Thus, you shouldn't use readonly if your server code isn't expecting a value from that control.

     It's been my experience that trying to hack something to get an acceptable presentation usually isn't worth it. I'd suggest you either change your CSS so that the fixed IE disabled text styling doesn't conflict with your underlying control style, or you use styled text in place of the control (since disabled controls aren't successful for submission anyways). Work with the browser and not against it.

  • +1 This is the correct answer, IMO. Either change your design slightly so that your disabled input looks okay in IE with the default text colour, or you can use a div which is styled how you want the disabled input to look. – BadHorsie Sep 11 '13 at 10:45
8

I had the same problem for <select> elements in IE10 and found a solution:

http://jsbin.com/ujapog/90

There is a Microsoft psuedo-element that allows the text color to be modified:

select[disabled='disabled']::-ms-value {
    color: #000;
}

The rule must be on it's own, because otherwise other browsers will ignore the whole rule due to syntax error. See http://msdn.microsoft.com/en-us/library/ie/hh869604(v=vs.85).aspx for other Internet Explorer only psuedo elements.

EDIT: The -ms-value psuedo-element was introduced in IE10 so won't work on earlier versions.

robocat
  • 5,293
  • 48
  • 65
  • That didn't seem to change the style in IE8. Still embossed grey on my machine. – Snekse Sep 05 '13 at 19:38
  • I had been looking for a solution and searched the almost the whole internet and couldn't find a solution for the disabled select box and this works! Thanks! – SED Jul 08 '15 at 12:40
  • 2
    I suspect it is probably better to use `select:disabled::-ms-value` (see https://developer.mozilla.org/es/docs/Web/CSS/:disabled ) or `select[disabled]::-ms-value` (however I don't have IE installed at the moment so haven't tested either, sorry). – robocat Jul 11 '15 at 05:02
4

Problem with IE that it cannot apply style for disabled control, that is why firstly we need to remove it from our textboxes, then add our style and disable focusing on control. I've tried also unbind('focus') but it didn't work.

So I used JQUERY for this:

var disabledControls = $("input:disabled, textarea:disabled");
    disabledControls.removeAttr('disabled');
    disabledControls.addClass("is-disabled");
    disabledControls.focus(function() {
    this.blur();
});

CSS class:

.is-disabled {
    background-color: #EBEBEB;
    color: black !important;   
}

Works perfectly in IE8\9 and Chrome...

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Schnapz
  • 1,208
  • 13
  • 10
  • Because you have removed the disabled attribute of the input, the value will be sent to the server when you submit the form. – BadHorsie Sep 11 '13 at 10:35
2

You can use the following to simulate disabled-field behaviour with these next lines:

// find all disabled fields
$("input:disabled, textarea:disabled, select:disabled").each(function(index){
    // add the class is_disabled
    $(this).addClass("is_disabled");
    // remove the attribut disabled
    $(this).removeAttr('disabled');
    // add new attribut readonly
    $(this).attr('readOnly', 'readOnly');
});
// on submit remove all fields which are still with class is_disabled
$('form').submit(function() {
    // find all fields with class is_isabled
    $("input.is_disabled, textarea.is_disabled, select.is_disabled").each(function(index){
        //  and remove them entirely
        $(this).remove();
    });
    return true;
});
// now don't let anyone click into the fields
// this will simulate the 'disabled' functionality
$('.is_disabled').click(function() {
  $('.is_disabled').blur();
});
dazz
  • 96
  • 1
  • 6
1

I guess you can write the css for that than going ahead with JQuery. I have not tested it out. But if you disable a textarea, it should take care of the styles automatically. Add this to your stylesheet and let me know....

input[disabled][type='text'], textarea[disabled]{ color: rgb(0,0,0); background-color:Silver;}
user007
  • 1,504
  • 2
  • 18
  • 51
1

I also looked for a simple, scriptless solution. The following worked for me in IE 8. The idea is to use background transparency and a filter to transform and colorize the text. This darkens the disabled text just enough to make it readable.

textarea[disabled], select[disabled], input[type='text'][disabled] {    
  /*required*/
  background-color:transparent; 
  filter: light;

  /*fine tuning*/
  font-weight:100;
  font-family: Tahoma;
  border: 1px solid;
  letter-spacing:1px;
}

The select doesn’t have a drop shadow, so make the text very thick to get a similar font-weight effect.

select[disabled] {
  font-weight:900;
}
Cody Guldner
  • 2,888
  • 1
  • 25
  • 36
Adi Katz
  • 548
  • 3
  • 9
0

input[disabled] and input[readonly] works in IE 7, 8 and 9 when you add this line to the html file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Anas
  • 9
  • 1
  • This is the only solution worked for me. I tried all other answers in this page and other questions, nothing helped me. – Siva Jun 18 '14 at 19:49
0

Afaicr in the HTML it looks a little something like <textarea disabled="disabled"> right? You could get away with textarea[disabled="disabled"] in IE7 (might not work in IE6 however).

fredrikholmstrom's answer is the best cross-browser solution.

Community
  • 1
  • 1
Ross
  • 46,186
  • 39
  • 120
  • 173
-1

Use a label overlay:

<asp:Label ID="lblTxtHider" runat="server" Text=""    CssClass="hidden" Enabled="false"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" CssClass="frmItem" Visible="false" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

Then in CSS:

.disabledColorChanger {
position: absolute;
font-family: Arial;
font-size: 13px;
margin-top: 3px;
margin-left: 4px;
}
.disabledColorChanger[disabled] {
    display:none;
}

And in code when setting or disabling:

private void SetEnabled(bool enabled)
{
   lblTxtHider.Enabled = !enabled;
   TextBox1.Enabled = enabled;
   lblTxtHider.Text = TextBox1.Text;
}

And the CORRECTED code for detecting ie:

<script>
    $(document).ready(function () {
        var isIE = /*@cc_on!@*/false || !!document.documentMode;
        if (isIE) {
            document.getElementById("<%=lblTxtHider.ClientID%>").className = "disabledColorChanger";
        }
    });
</script>

Just set the label = the input value. Works for me.

Mike
  • 71
  • 1
  • 2
-11

You have two options (since IE 6/7 doesn't support the :disabled css-selector)

  1. Use jQuery (or some other js-lib of your choosing) and apply a class to all disabled input elements, say: $("input:disabled, textarea:disabled").addClass("is-disabled");
  2. Add the "is-disabled" class manually on the server side, this is of course only viable if you know before hand which elements will be disabled and which won't.
thr
  • 19,160
  • 23
  • 93
  • 130
  • 1
    no idea why this answer is selected as it is incorrect. See the upvoted answer from bobince below which is the actual correct answer. – Andy Mar 12 '12 at 18:03