1

i'm basically JavaScript newbie and I'm trying to resolve this problem of mine for quite a while. So,i'm doing JS school project and I need to make connection between checkbox and text form. If checkbox is not checked, text form should be disabled and vice versa. This is piece of code I have written:

function cbtf() {
    if (document.getElementById('checkbox').checked==false) {
        document.getElementById('textform').disabled=true;
    }
}

Can anyone write a new code ? That would be much of a help.

9
  • did it work? If not, what occurred? Commented May 12, 2018 at 20:19
  • 1
    Where's the HTML? Commented May 12, 2018 at 20:20
  • Please be more detailed about your problem - with examples and what you did, so others can help you. Commented May 12, 2018 at 20:20
  • @Terry - Why remove the snippet? Commented May 12, 2018 at 20:22
  • 1
    Tako update your question Commented May 12, 2018 at 20:25

3 Answers 3

1

Simply attach a method to checkbox's onclick handler:

function enableElement(id, enable) {
    document.getElementById(id).disabled=!enable;
}
<label>
  <input 
    type="checkbox" 
    onclick="enableElement('textform', this.checked)" 
  />
  ENABLE
</label> 

<br/>

<textarea id="textform" style="width:100%; height:200px" disabled>
  THIS IS TEXTAREA WITH ID "textform"
</textarea>

or another simplification without creating special one-liner method - just define Your will directy in onclick event:

<label>
  <input 
    type="checkbox"
    onclick="document.getElementById('textform').disabled = !this.checked" 
  />
  ENABLE
</label> 

<br/>

<textarea id="textform" style="width:100%; height:200px" disabled>
  THIS IS TEXTAREA WITH ID "textform"
</textarea>

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

1 Comment

That worked num8er,thank u so much <3 ! Thank you guys for help! Bear in mind that this is my first question on this site and sorry for stupidity on certain things !
0

You can add a click event to the checkbox, and assign it's check state to the disabled property of the TextBox.

document.querySelector('input[type=checkbox]').onclick = function(e) {
  document.querySelector('input[type=text]').disabled = e.target.checked;
};
<input type="checkbox" name="">
<input type="text" name="">

Comments

0

You won't get that to work unless you attach an event to the checkbox, so I would suggest something like this:

var textbox = document.getElementById('textform');
var checkbox = document.getElementById('checkbox');

checkbox.addEventListener("change", function() {
    if (checkbox.checked) {
        textbox.disabled = false;
    } else {
        textbox.disabled = true;
    }
})

1 Comment

Probably want to listen to the input or change event ;) also, if the checkbox is going to be a toggle, then you need to add an else statement to (which re-enables the textbox) if it is unchecked.

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.