Hi everyone, I’m new guy in here, I’m learning about JavaScript, it’s not easy. I 'm trying to make checkbox stay checked after reload or flip other site in iframe in html. LocalStorage is not really work very well, it response is work, but it didn’t save all checked in all checkbox, then come back to load it back to all checkbox. Can you help? here my code.
Thanks,
Gary
<script>
function onClickBox() {
let checked = $("#box").is(":checked");
localStorage.setItem("checked", checked);
$("#msg").text("Checkbox is checked: " + $("#box").is(":checked"));
}
function onReady() {
$("#msg").text("Loaded.");
let checked = "true" === localStorage.getItem("checked");
$("msg").prop('checked', checked);
$("#box").click(onClickBox);
}
$(document).ready(onReady);
</script>
<form>
<div id="msg"></div>
<table>
<tr><th colspan="3">Spatial Verbs</th></tr>
<tr><td>Go/Walk-To/From</td>
<td><input class="single-count" type="checkbox" id="box" name="chkboxGrp0[]" value="1"></td>
<td class="vert-middle">0</td></tr>
It means that the checkbox should be empty based on what you are trying to do. However if you were to click the box and reload the page your checked will be true however your input will not be checked. So the issue lies in how you are setting your DOM element to be in a checked state.