0

I'm trying to make a html5 form with different input fields, with a checkbox that can enable/disable input fields.

When the checkbox is selected, it should be possible to enter data into an input field. When the checkbox isn't selected, it shouldn't be possible to enter data to the input field.

I already tried this (just to try if enabling/disabling works):

<script>
document.getElementById("checkbox").onclick = function() {
    document.getElementById("inputfield").disabled=true;
}
</script>

The input field is disabled when the user clicks on the checkbox, so disabling fields works but I don't know how it works with selecting the checkbox and re-enabling.

This is my first time using javascript so any help is greatly appreciated!

2 Answers 2

2

You check the state of the checkbox

document.getElementById("inputfield").disabled= this.checked;
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks! Also got another solution:

function active()
{
  if(document.getElementById('checkbox').checked)
     document.getElementById('inputfield').disabled=false;

  else
     document.getElementById('inputfield').disabled=true;
}

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.