0

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.

5
  • you need "check" to be posted in order for php to show the check. but otherwise it looks fine. the fiddle you posted is functional as well. If all you're looking for is some code to remember the state you could use localStorage to remember the value Commented May 15, 2013 at 18:53
  • 1
    I think you need to check its state on loading document. Something like if (cb.checked) { do_something(); } Commented May 15, 2013 at 18:54
  • right, there are really two questions here. 1) "how do I maintain state" 2) "how do I trigger a function to run code when the state is true Commented May 15, 2013 at 18:57
  • yes that is what I need. I'll scout the web about the localStorage cause for the moment I am clueless. If you can be more explicit please. Thanks Commented May 15, 2013 at 18:58
  • didn't even noticed all these comments. Commented May 15, 2013 at 18:59

1 Answer 1

3

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();
});
Sign up to request clarification or add additional context in comments.

2 Comments

@Shanimal: Typo. Thanks for spotting that one for me. EDIT: Haha
right. I know its messy but I am tired and my back is killing me so Im just throwing words around here. I'll work with this. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.