4

"readonly" property not working. Please check my code below http://jsfiddle.net/KZArL/

Even after setting readonly still i can change value...I am using the below code

$('select').prop('readonly','readonly');

If i disable post won't carry disabled value...

Reddy
  • 1,327
  • 3
  • 23
  • 44

4 Answers4

7

AFAIK, you cannot make a select read only, what you need to do is disable it:

$('select').prop('disabled', true);

Bare in mind that disabled form fields are not submitted with the form.

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
  • Though others mentioned disabled, +1 for mentioning it becomes invisible on form submission. – Brad Christie Jul 27 '11 at 15:33
  • 1
    you could do `$('select').closest('form').submit(function(){ $('select').prop('disabled', false);});` to enable it before submit and have more or less the same than readonly – Enki Jul 27 '11 at 15:45
  • @enki No point as even if it's disabled it can be changed with simple tools. – Dunhamzzz Jul 27 '11 at 15:51
3

<select> doesn't have a readonly attribute, which is probably the issue you're seeing.

See the MDN on <select> for more details.

On another question though, I just made a very simple selection change prevention plugin for jQuery if you're interested.

Community
  • 1
  • 1
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • 1
    @Enki: Indeed, but that makes it not show up on a form submission. Given it's a form element, I feel that wouldn't be desired. – Brad Christie Jul 27 '11 at 15:34
  • well using value of a readonly is not good I think ( since we think user can't change it but the user can ) but if it's needed the field could be enabled before submit ( or the value copied in an hidden field ) – Enki Jul 27 '11 at 15:42
  • @Enki: Or, `$('select>option:not([selected])').css('display','none');` – Brad Christie Jul 27 '11 at 15:53
0

You should use disabled:

 $('select').prop('disabled', true);
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0

try this:

$('select').attr('readonly', true);
Nikola K.
  • 7,093
  • 13
  • 31
  • 39
Vinay
  • 1,016
  • 8
  • 22
  • 1
    Anywhere from 4-7 minutes after it's been determined there is no readonly attribute you come up with this solution. Interesting. – Brad Christie Jul 27 '11 at 15:39