SpectralEdge
SpectralEdge

Reputation: 201

Toggle visibility on load if checkbox checked

Tried to find similar answers, but did not find one with this exact problem. Sorry if I missed one in my search.

I have this code

    jQuery(function (jQuery){
      jQuery("#discount_ongoing").click(enable_date);
    });

    function enable_date() {
    jQuery('#discount_start').toggle();
    jQuery('#discount_end').toggle();
    }

It simply toggles the visibility of two calendars. As it is, it just turns the calendars on and off when the checkbox is clicked, which is fine until I save the state of the checkbox, then when I load the page with it already checked, obviously the toggle will do me no good because it works in the opposite direction.

How do I check the state of the checkbox so the calendars start off in the correct state when the page loads? Or, is there a better way to do this?

Upvotes: 0

Views: 109

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388446

Try

jQuery(function (jQuery){
    jQuery("#discount_ongoing").click(enable_date).triggerHandler('click');
});

function enable_date() {
    jQuery('#discount_start, #discount_end')[this.checked ? 'show' : 'hide']();
}

Demo: Fiddle

Upvotes: 3

Related Questions