0

I want to know what is the best way to add a style in the code below so that the readonly textboxs are coloured grey?

$("[class*=q" + i + "_mark]").val(t_marksHtml).attr('readonly', true);
3
  • 1
    readonly boxes are greyed out per default in decent browsers? Commented Dec 13, 2012 at 12:27
  • Are we classing firefox as decent browsers lol, because it isn't doing it in firefox. But that maybe because in my code depending on situation, some text inputs are readonly and some are not Commented Dec 13, 2012 at 12:33
  • 2
    @Christoph only disabled inputs are greyed out by default Commented Dec 13, 2012 at 12:33

6 Answers 6

4

Try

 $("selector for textbox").addClass("read-only-state");

and apply css in .read-only-state class

 .read-only-state{
  color:#333;
  background:grey;
  }
Sign up to request clarification or add additional context in comments.

Comments

2

This can be done in pure CSS:

textarea[readonly]{
  color: grey;
}

or (better yet) left to the browser.

Comments

1

Why don't you use pure CSS?

input[readonly]
{
    background-color: grey
}

DEMO: http://jsfiddle.net/DqjAC/

Comments

1

CSS

textarea[readonly]
{
    background: #999;
}

Comments

1

just use attribute selector

$("input:[readonly]").css({"background":"#666"});

But its better to do with CSS only as

input[readonly]{
  background: #666;

}

Comments

0

basically, use this for all html tags with readonly attribute.

$("[readonly]").css("background-color","#CCC");

only input tags

$("input:[readonly]").css("background-color","#CCC");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.