Skip to content

Instantly share code, notes, and snippets.

@sivaprabug
Last active March 15, 2016 09:43
Show Gist options
  • Select an option

  • Save sivaprabug/c49873f647fcca9c7fe3 to your computer and use it in GitHub Desktop.

Select an option

Save sivaprabug/c49873f647fcca9c7fe3 to your computer and use it in GitHub Desktop.
Checkbox value stored in local storage
<!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