1

I've a textbox with readonly="readonly" that means I can not edit it. But what I want is to make this textbox editable when user double clicks on it.

What I've tried yet is:

<input size="10" readonly="readonly" ondblclick="setEditable(this)"/>

and in JavaScript:

 function setEditable(i){
     i.readonly = false;
 }

But this does not worked. So how can I make a textbox editable, which is readonly, when user double clicks on it?

3 Answers 3

6

Update:

To make it readonly again:

var el = document.getElementById('txt');
el.onblur = function(){
  this.setAttribute('readonly');
};

You can do this:

<input size="10" readonly="readonly" id="txt" />

JS:

var el = document.getElementById('txt');
el.ondblclick = function(){
  this.removeAttribute('readonly');
};
Sign up to request clarification or add additional context in comments.

3 Comments

Thnx your answer works fine. I tried to accept your answer but it says "You can accept an answer in 5 minutes". So have to wait for 5 minutes. Also do you know answer for my q2 which is related to CSS.
@Harry Joy: You should post your second question separately to get targeted answers :) I will check out then if i can answer or others may come up with answers :)
0

as above pure javascript example by sarfraz try to make your javascript unobtrusive much better practice

Also as a lot of people do if you have jquery on page now you can use that to do same

In jquery

$('#txt').removeAttr("readonly");

1 Comment

thnx for reply but I use Javascript.
0

To make text field editable

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

To make text field Uneditable

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.