Disable readonly to text box onclicking the button

后端 未结 3 485
醉话见心
醉话见心 2021-01-04 06:07

I have used "readonly" attribute to the textbox which makes the textbox non editable and may i know how to disable the readonly attribute on clicking the button. c

3条回答
  •  北海茫月
    2021-01-04 06:53

    You can do two things:

    • Remove the readonly (case insensitive) attribute, using removeAttribute
    • Set the readOnly (case sensitive) property to false

    HTML:


    JS (option 1): [Demo]

    document.getElementById('myButton').onclick = function() {
        document.getElementById('myInput').removeAttribute('readonly');
    };
    

    JS (option 2): [Demo]

    document.getElementById('myButton').onclick = function() {
        document.getElementById('myInput').readOnly = false;
    };
    

提交回复
热议问题