Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set textbox to readonly and background color to grey in jquery

Tags:

Good day,

I would like to make a text box in my jsp to become readonly and its background color to grey like disable 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?

like image 758
Panadol Chong Avatar asked Jan 10 '14 02:01

Panadol Chong


People also ask

How to set readonly textbox using jQuery?

How can you make a textbox readonly using JQuery? $('#TextBoxId'). attr('readonly', 'true'); 1.

How can change background color of textbox in jQuery?

To change the text color with jQuery, use the jQuery css() method. The color css property is used to change text color.

How can set background color in jQuery?

To set the background color using jQuery, use the jQuery css() property. We will set background color on mouse hover with the jQuery on() method.

How do you make a selection readonly in jQuery?

$('#cf_1268591'). attr("readonly", "readonly");


1 Answers

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.

like image 89
Alex Avatar answered Sep 19 '22 12:09

Alex