Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Javascript function only when checkbox is NOT checked

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();
like image 372
Jeff Prachyl Avatar asked Aug 03 '12 17:08

Jeff Prachyl


2 Answers

document.getElementById('icd').onchange = function() {
    if ( document.getElementById('icd').checked === false ) {
        planhide();
    }
};​
like image 160
David G Avatar answered Sep 19 '22 23:09

David G


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()/>
like image 29
vivek_jonam Avatar answered Sep 22 '22 23:09

vivek_jonam