How to set readonly property of a text field in javascript

I have a text field whose property I need to set as readonly ... how to set it? I tried

document.getElementbyid("txtbox").readonly=true; document.getElementbyid("txtbox").disable=true; document.getElementbyid("txtbox").setattribute("readonly","readonly"); 

all this does not work for me. Disable works, but it passes control values ​​as null to the database ... again this is a problem for me ..

+4
source share
4 answers

You can set an ASP.NET text field using javascript only. Here's how:

 <script type="text/javascript"> function setReadOnly(){ var textbox = document.getElementById("TextBox1"); textbox.readOnly = "readonly";//readOnly is cese-sensitive } <body onload=" setReadOnly ()"> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> </form> </body> 

Hope this helps you.

+6
source

You have numerous code issues in your question. The very first example will work if you got the property names correctly:

 document.getElementById("txtbox").readOnly = true; 

Note the uppercase letters ( getElementById instead of getElementById and readOnly instead of readOnly ).

Here is a working example .

As for your second attempt, the disabled property, not disable . And your 3rd attempt, the setAttribute method, not setAttribute . As you can see, JavaScript is case sensitive!

+5
source

The right way:

 document.getElementById("txtbox").setAttribute("readonly", "true"); 

Or for jQuery enthusiasts:

 $("#txtbox").attr("readonly", true); 
+4
source
 <script type="text/javascript"> function loading() { var input_text = document.getElementById("text"); input_text.setAttribute("readOnly", true); } 

Works fine for me ....... use camelcase with the property ....... readOnly not as read only .....

+2
source

Source: https://habr.com/ru/post/1415341/


All Articles