0

I have some form which is editable in the same page. First attribute for each textbox is readonly, and when I press the edit button, it will become editable, using this syntax

$('input[readonly="readonly"]').removeAttr("readonly").prop("enabled",true);

when i press the save button, i want it become readonly again. I've trying to change it to

 $('input[readonly="readonly"]').attr("readonly").prop("disable",true);
  and
 $('input[readonly="readonly"]').attr("readonly").prop("readonly",true);

but it won't change into readonly again.

Is there any proper way to do this?

Philip Gullick
  • 995
  • 7
  • 22
randytan
  • 1,029
  • 5
  • 23
  • 52

3 Answers3

0

readonly attributes was removed when you press the edit button.

so, $('input[readonly="readonly"]') doesn't match anything.


For example,

$('input[type="text"]').attr("readonly", "readonly");

will set all textboxes to readonly.

fliedonion
  • 912
  • 8
  • 13
  • @randytan if you want to specify the textbox. you need set class, name or id to target textboxes. – fliedonion Jul 12 '13 at 11:58
  • Check my answer, maybe you want to add an ID for your textbox and use that to select. Choosing the selectors depends on your HTML – Lzh Jul 12 '13 at 11:58
  • okay, i just use type="text" and it works. I prefer not to add additional ids or name to make it disable, because, in my page there are more than 100 text inputs. thanks guys – randytan Jul 12 '13 at 12:03
0

you need to

$('<your-selector>').prop("disable",true).attr("readonly", "readonly");

because calling .attr("readonly") just returns the value of the attribute readonly, here you need to set the readonly property value for that you need to pass two arguments to .attr()

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

In $('input[readonly="readonly"]').attr("readonly").prop("disable",true);, the selector that you are using is looking for an input that is already readOnly I don't think that you want to make a readOnly input readOnly.

Give it an ID or find another way to select it then do $("#myInput").attr("disabled", "disabled"). Tested on chrome.

Lzh
  • 3,585
  • 1
  • 22
  • 36