18

I'm trying to figure out how to call a Javascript function only when a checkbox is NOT checked.

Here is my checkbox:

<input type="checkbox" id="icd" name="icd" value="icd" />

Here is the name of the function:

planhide();

4 Answers 4

28
document.getElementById('icd').onchange = function() {
    if ( document.getElementById('icd').checked === false ) {
        planhide();
    }
};​
Sign up to request clarification or add additional context in comments.

Comments

3

Include onchange option in the input tag and then add an intermediate function that checks and calls planhide() accordingly as follows:

<input type="checkbox" id="icd" name="icd" value="icd" onchange=check()/>

Then define the check() to do check the state and call the function as follows:

function check()
{
if(document.getElementById("icd").checked==false)
planhide();
}

Also instead of onchange you can also use onclick on the submit button option to call the check() function as like follows:

<input type="button" onclick=check()/>

Comments

1

$(document).ready(function () {
   $('#icd').change(function () {
      if (!this.checked) {
          planhide();
    }
   });
});

1 Comment

You should add some form of explanation, try and avoid code only answers as they are of limited use to the OP and future visitors
0

just register an onchange handler on your input, check the 'checked' property when the handler is call, and call the method if checked is false.

Here is a fiddle.

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.