Mimi
Mimi

Reputation: 389

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) {
        loadgrdSubgroupItem();
    }
};

From where should I call IsSubGroupNeeded function?

Upvotes: 0

Views: 74

Answers (1)

Transformer
Transformer

Reputation: 3760

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.

$(document).ready(function(){
    //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
    }
});

});

Upvotes: 1

Related Questions