fatherazrael
fatherazrael

Reputation: 5977

How run a statement on change as well as on load event in JQuery?

I want to disable a button; on page load as well as on checkbox change

Following is JQuery:

var myquery= {

init: function () {
     $('#buttonName').prop('disabled', !$("input[name='checkboxName'").is(':checked')); 
     // Run on page load

    $("input[name='checkboxName'").change(function() {
        $('#buttonName').prop('disabled', !$(this).is(':checked'))
    }); // Run on event
  }
};
$(document).ready(myquery.init());

How can i handle onLoad as well as change events together without changing inside init only?

Upvotes: 0

Views: 42

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

I think the problem is how you are adding the dom ready handler, you are invoking the init method before the dom ready and is passing the value returned by it(undefined) as the dom ready handler

Also you can trigger the change handler on page load, to update the status

var myquery = {

    init: function () {
        $("input[name='checkboxName'").change(function () {
            $('#buttonName').prop('disabled', !$(this).is(':checked'))
        }).change();
    }
};
$(document).ready(myquery.init);

Upvotes: 2

Related Questions