user4044890
user4044890

Reputation:

How to call 2 functions on onclick of a checkbox?

I have a checkbox, which when clicked some operations are performed. I am not gonna specify what operation it really performs...for the time being let's think some mathematical operations (simple addition/ multiplication) are performed when I check that checkbox.

<input type="checkbox" name="chk_ceil" id="chk_ceil" onclick="thecheckfunction();"> What the Check

So, on onclick property of the checkbox, I call a JavaScript function.

Function definition:

<script>
    function thecheckfunction()
    {
    // SOme operations (operation A & B performed)    
    alert('checkbox is checked');
    }
</script>

And now comes the crazy part. When I uncheck the checkbox, the function is called again & the operations are performed once again, which I don't wanna happen as it is not supposed to perform an operation when I uncheck the checkbox. I thought there must be a way to tackle this issue. Maybe by calling two functions on the onclick property of the checkbox. One when the checkbox is checked & one when checkbox is unchecked.

Help???

Upvotes: 1

Views: 2127

Answers (1)

Amit
Amit

Reputation: 15387

Use as

 function thecheckfunction(){
    var chk_ceil= document.getElementById("chk_ceil");       
    if(chk_ceil.checked == true){
         alert('checkbox is checked');
    }
}

Upvotes: 4

Related Questions