1

I am having trouble with an if-statement. In this statement i want to check if a checkbox is checked true (or isSelected) then it shall proceed with the instruction to disable a textarea. This part is working fine. But unfortunately if the checkbox is unchecked it still disabling the textarea, but it shouldn't. It should leave the textarea enabled.

function auswahl_bestaetigen(){

    tweet = document.getElementById("chk_tweet")
    twitter = document.getElementById("twitter")

    if (tweet.isSelected = true)
                twitter.disabled = true
    else if (tweet.isSelected = false) 
                twitter.disabled = false    
}

i hope someone can help me with this problem. Thanks in advance :)

4
  • 1
    if (tweet.isSelected = true) should be if (tweet.isSelected). = means assignment. Also, this is not Java. Commented Oct 20, 2017 at 19:46
  • 1
    There is also no isSelected property Commented Oct 20, 2017 at 19:55
  • The question is not very clear altogether. It is implied that we're talking about JavaScript and HTML DOM but it would be better to be explicit. The HTML example would help too. Ideally, you'd present a working or broken test on some of the on-line snippet sites like jsfiddle.net . To the topic itself, I'll recommend W3Schools w3schools.com/jsref/dom_obj_checkbox.asp as the question has been answered pretty much. Note, the down-vote was not mine. You get down votes when the quality of your question is in trouble. Commented Oct 20, 2017 at 20:10
  • Downvoting questions like this is just silly. Not getting an answer should be a good enough indication that the question is posted in the wrong format or in the wrong place. I don't get why some people find it necessary to downvote as on top of that. Commented Oct 20, 2017 at 20:53

1 Answer 1

4

The property to test whether an <input type="checkbox"> is checked is called checked. So your code should be:

if (tweet.checked) {
  twitter.disabled = true
} else {
  twitter.disabled = false
}

Or even shorter:

twitter.disabled = tweet.checked

Here's a simple demo:

var tweet = document.getElementById('chk_tweet')
var twitter = document.getElementById('twitter')
tweet.addEventListener('change', function () {
  twitter.disabled = tweet.checked
});
#twitter[disabled] {
  background: grey;
}
<input id="chk_tweet" type="checkbox" />

<textarea id="twitter"></textarea>

https://jsfiddle.net/foxbunny/m4oda3zy/

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

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.