qqqqqqqqqqq
qqqqqqqqqqq

Reputation:

Setting the Textbox read only property to true using JavaScript

How do you set the Textbox read only property to true or false using JavaScript in ASP.NET?

Upvotes: 19

Views: 134496

Answers (6)

SteveCinq
SteveCinq

Reputation: 1963

I find that document.getElementById('textbox-id').readOnly=true sometimes doesn't work reliably.

Instead, try:

document.getElementById('textbox-id').setAttribute('readonly', 'readonly') and document.getElementById('textbox-id').removeAttribute('readonly').

A little verbose but it seems to be dependable.

Upvotes: 6

JDGuide
JDGuide

Reputation: 6525

Try This :-

set Read Only False ( Editable TextBox)

document.getElementById("txtID").readOnly=false;

set Read Only true(Not Editable )

var v1=document.getElementById("txtID");
v1.setAttribute("readOnly","true");

This can work on IE and Firefox also.

Upvotes: 2

Sridhar
Sridhar

Reputation: 9084

You can try

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

Upvotes: 36

Henry Gao
Henry Gao

Reputation: 4936

it depends on how you trigger the event. the key you are looking is textbox.clientid.

x.aspx code

<script type="text/javascript">

   function disable_textbox(tid) {
        var mytextbox = document.getElementById(tid);
         mytextbox.disabled=false
   }
</script>

code behind x.aspx.cs

    string frameScript = "<script language='javascript'>" + "disable_textbox(" + tx.ClientID  ");</script>";
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "FrameScript", frameScript);

Upvotes: 0

Chris
Chris

Reputation: 1836

document.getElementById('textbox-id').readOnly=true should work

Upvotes: 3

Soufiane Hassou
Soufiane Hassou

Reputation: 17750

Using asp.net, I believe you can do it this way :

myTextBox.Attributes.Add("readonly","readonly")

Upvotes: 0

Related Questions