Working with localstorage have a solution implemented for my site. However, despite checkboxes remaining checked on page refresh their accompanying functions like revealing or enabling an input field do not occur.
How can I make it so that hidden sections remain revealed appropriately and disabled fields are enabled when accompanying checkbox remains clicked on page refresh?
Here's a mockup of what I'm working with in jsfiddle: https://jsfiddle.net/bpmnruhg/
JS:
$(document).ready(function(){
$('#sub-check').change(function(){
if(!this.checked)
$('#submitter').fadeOut('slow');
else
$('#submitter').fadeIn('slow');
});
});
$(document).ready(function () {
function init() {
if (localStorage["pi-name"]) {
$('#pi-name').val(localStorage["pi-name"]);
}
if (localStorage["pi-jhed"]) {
$('#pi-jhed').val(localStorage["pi-jhed"]);
}
if (localStorage["pi-email"]) {
$('#pi-email').val(localStorage["pi-email"]);
}
if (localStorage["sub-name"]) {
$('#sub-name').val(localStorage["sub-name"]);
}
if (localStorage["sub-jhed"]) {
$('#sub-jhed').val(localStorage["sub-jhed"]);
}
if (localStorage["sub-email"]) {
$('#sub-email').val(localStorage["sub-email"]);
}
}
init();
});
$('.stored').keyup(function () {
localStorage[$(this).attr('name')] = $(this).val();
});
var checkboxValues = JSON.parse(localStorage.getItem('checkboxValues')) || {},
$checkboxes = $("input:checkbox");
$checkboxes.on("change", function(){
$checkboxes.each(function(){
checkboxValues[this.id] = this.checked;
});
localStorage.setItem("checkboxValues", JSON.stringify(checkboxValues));
});
// On page load
$.each(checkboxValues, function(key, value) {
$("#" + key).prop('checked', value);
});
HTML: Name:
<label for="pi-jhed">JHED ID: </label>
<input class="stored" name="pi-jhed" id="pi-jhed" type="text" required />
<label for="pi-email">Email: </label>
<input class="stored" name="pi-email" id="pi-email" placeholder="you@example.com" type="email" required />
<label for="sub-check" style="display:inline">Is the person making the submission different from the PI? </label>
<input type="checkbox" class="stored" name="sub-check" id="sub-check" onchange="document.getElementById('sub-name').disabled = !this.checked; document.getElementById('sub-jhed').disabled = !this.checked; document.getElementById('sub-email').disabled = !this.checked;" />
<br />
<div class='hidden' id="submitter">
Submitter:
<label for="sub-name">Name: </label>
<input class="stored" name="sub-name" id="sub-name" placeholder="Last Name, First Name" type="text" required disabled />
<label for="sub-jhed">JHED ID: </label>
<input class="stored" name="sub-jhed" id="sub-jhed" type="text" required disabled />
<label for="sub-email">Email: </label>
<input class="stored" name="sub-email" id="sub-email" placeholder="you@example.com" type="email" required disabled />
</div>
CSS:
.hidden {
display:none;
}