4

How do you use the jQuery Cookie plugin to ensure a checked checkbox remains checked when refreshing the page or leaving and returning to the page?

1 Answer 1

4

If using the $.cookie jQuery plugin...

var checkbox = $('#your-form :checkbox:first'),
    checkboxCookieName = 'checkbox-state';

checkbox.prop('checked', +$.cookie(checkboxCookieName));

checkbox.click(function() {
   $.cookie(checkboxCookieName, +this.checked);
});
Sign up to request clarification or add additional context in comments.

3 Comments

There is unwanted + while setting the checkbox checked prop.
@ShankarSangoli How is it unwanted? I want to store '0' or '1' for ease of converting back from string to something falsy.
Also note that .prop() was added in jQuery 1.6. If you using more older plugin it can be replaced with .attr()

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.