Last active
March 15, 2016 09:43
-
-
Save sivaprabug/c49873f647fcca9c7fe3 to your computer and use it in GitHub Desktop.
Checkbox value stored in local storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>jQuery Test page</title> | |
| <script type="text/javascript" src="libs/jQuery/jquery-2.1.0.js"></script> | |
| <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> | |
| <script> | |
| if (typeof jQuery != 'undefined') { | |
| console.log(jQuery.fn.jquery); | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <form>Favorites | |
| <input type="checkbox" name="favorites" value="1"> | |
| </form> | |
| <script> | |
| $(function() { | |
| var data = localStorage.getItem("favorite"); | |
| if (data !== null) { | |
| $("input[name='favorites']").attr("checked", "checked"); | |
| } | |
| }); | |
| $("input[name='favorites']").click(function() { | |
| if ($(this).is(":checked")) { | |
| localStorage.setItem("favorite", $(this).val()); | |
| } else { | |
| localStorage.removeItem("favorite"); | |
| } | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment