0

In the jquery

According to some condition I make one field as read only with below code

$("#name").attr("readonly", true);

it is working fine

but when i try to disable it by using below codes

$("#name").attr("readonly", false);
           OR
$("#name").attr("disabled", false);

It is not getting disabled. How can achieve it

Sachu
  • 7,003
  • 7
  • 44
  • 82
  • $("#name").attr("readonly", ""); removes the attribute. – Riddler Jul 02 '15 at 06:47
  • _but when i try to disable it_ != `disabled = false` O.o, also you should be using [`.prop()`](http://api.jquery.com/prop/) instead of `.attr()` – Andreas Jul 02 '15 at 06:47
  • Check these two jsfiddles: [jsfiddle1](http://jsfiddle.net/3mG8g/1/), [jsfiddle2](http://jsfiddle.net/obmerk99/6jFMf/20/) ... `disabled` should have `disabled` or `null` for the `$...attr()` – mk117 Jul 02 '15 at 06:49
  • I closed this a duplicate. The other question is about `disabled`, not `readonly`, but it should be the same. – Kobi Jul 02 '15 at 06:50
  • thanks all of u for the help – Sachu Jul 02 '15 at 07:05

1 Answers1

1

The value of the disabled attribute does not matter. If it is in the attribute list, the element will be disabled regardless of whether it is false or true or empty. You need to remove the attribute to enable it back.

Use $("#name").removeAttr('disabled') is re-enable the element.

techfoobar
  • 61,046
  • 13
  • 104
  • 127