Klaus
Klaus

Reputation: 1

Checkbox to stay checked after refresh/submit and

if so to trigger a js function. I can make a checkbox stay checked with value and checked="checked" on page refresh and for my submit the following...

 <input type="checkbox" name="check" value="checked" 
 <?php if($_POST['check'] ==    'checked'): ?>checked="checked"<?php endif; ?> />

thou neither of this remember to trigger the js function.

Here is my function and what I try to accomplish. http://jsfiddle.net/melbourne/s5AXZ/

My only choices are what Im asking you or to show and post new comments via ajax.. still the refresh problem will remain but the refresh will not be needed so much. In a nutshell Im sticking with my first question since ajax posting and showing new comments its out of my league. Thanks.

Upvotes: 0

Views: 3627

Answers (1)

OptimusCrime
OptimusCrime

Reputation: 14863

Your question is kind of messy, but from what I understand do you wish to fire the .change-method if the checkbox is checked at load.

I'd suggest you just do the same php-if on the parent-element (the one you are adding the night-class to).

Something like:

<?php if($_POST['check'] == 'checked') echo 'class="night"'; ?>

It is much better to fix this at serverside when you already know the changes you wish to do.

BUT, if you want to do this with only js, you can do it like this:

function initBackground() {
    if ($('#btn').is(':checked')) {
        $('#btn').parent().toggleClass('night');
    }
}

$(document).ready(function () {
    initBackground();
});

Upvotes: 3

Related Questions