Mimi Mimi - 10 months ago 50
Javascript Question

How to call a function in javascript when a HTML Checkbox is checked without onclick or onchange

I want to call a function when checkbox value is true whether it is user input or retrieved from database. How to do it?
My Code

function IsSubGroupNeeded() {
var Subgrp = document.getElementById('myCheck').checked;

if (Subgrp === true) {

From where should I call IsSubGroupNeeded function?

Answer Source

Here you go. for checkbox data coming from database on document.ready will do that for you, but for user input in an already loaded page you must go with either click or change and then check if its currently checked. the if(this.checked) will do that.

    //on document load 
    if ($('input#mycheck').is(':checked')) {
        //cal the function
// you have to do a check on when user input (click)
$("input#mycheck").change(function() {
    if(this.checked) {
        //call the function