Set textbox to readonly and background color to grey in jquery
Solution 1
there are 2 solutions:
visit this jsfiddle
in your css you can add this:
.input-disabled{background-color:#EBEBE4;border:1px solid #ABADB3;padding:2px 1px;}
in your js do something like this:
$('#test').attr('readonly', true);
$('#test').addClass('input-disabled');
Hope this help.
Another way is using hidden input field as mentioned by some of the comments. However bear in mind that, in the backend code, you need to make sure you validate this newly hidden input at correct scenario. Hence I'm not recommend this way as it will create more bugs if its not handle properly.
Solution 2
As per you question this is what you can do
HTML
<textarea id='sample'>Area adskds;das;dsald da'adslda'daladhkdslasdljads</textarea>
JS/Jquery
$(function () {
$('#sample').attr('readonly', 'true'); // mark it as read only
$('#sample').css('background-color' , '#DEDEDE'); // change the background color
});
or add a class in you css with the required styling
$('#sample').addClass('yourclass');
Let me know if the requirement was different
Solution 3
If supported by your browser, you may use CSS3 :read-only
selector:
input[type="text"]:read-only {
cursor: normal;
background-color: #f8f8f8;
color: #999;
}
Panadol Chong
Updated on July 19, 2022Comments
-
Panadol Chong almost 2 years
Good day,
I would like to make a text box in my
jsp
to becomereadonly
and its background color to grey likedisable
in Jquery. The following is my code :if(a) $('#billAccountNumber').attr('readonly', 'true');
I not prefer using
attr('disable', 'true');
, because the value will become null when I submit form. Any ideas instead of write second line of code to change the style?