1

I made a very easy one HTML input column. I added JavaScript to it with a checkbox in it. When I clicked check box checked I want write the text or unchecked the text box read only how to solve my problem. Give for example for my code

<input type="text" id="inputID" value="abc"></input>

    <input type="checkbox" id="myCheck" checked>

    <script>
   document.getElementById('inputID').readOnly = true;

    </script>
0

1 Answer 1

1

Add an event listener to the change event on the checkbox.

<input type="text" id="inputID" value="abc" readonly></input>
<input type="checkbox" id="myCheck" >
<script>
    var checkbox = document.getElementById('myCheck');
    checkbox.addEventListener('change', function() {
        document.getElementById('inputID').readOnly = !this.checked; 
    });
</script>

Working fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

working.. but first time i want check box unchecked so read only. when i checked the check box i want write the text
Change the initial attributes on the the HTML and invert the change logic. I've updated the answer

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.