Make text box editable using JavaScript

后端 未结 3 603
既然无缘
既然无缘 2021-01-24 17:56

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.

Wha

相关标签:
3条回答
  • 2021-01-24 18:30

    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');
    };
    
    0 讨论(0)
  • 2021-01-24 18:32

    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");

    0 讨论(0)
  • 2021-01-24 18:37

    To make text field editable

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

    To make text field Uneditable

    document.getElementById("TextFieldId").readOnly=false;
    
    0 讨论(0)
提交回复
热议问题