I want to store the value of a checkbox (either on/true or off/false) in a SessionStorage variable.
This is the checkbox to save:
<label class="switch pull-right">
<input name="SeleccionarTodos" type="checkbox" id="SeleccionarTodos" onclick="checkAll()" />
<span class="slider round vista"></span>
</label>
I thought of it like this to set it, with the name "Select All".
let selectAll = $(('#SeleccionarTodos').checked);
sessionStorage.setItem("SeleccionarTodos", selectAll);
And for when I return to sight I bring it like this:
$(document).ready(function () {
$('#SeleccionarTodos').checked = sessionStorage.getItem("SeleccionarTodos");
}
Obviously I'm doing something wrong and that's why it doesn't work for me.
The problem is that you are not using the correct code to get the state of the
checkbox
.You can listen to the event
change
of that element, causing its state to be saved insessionStorage
with something like this:$(this)
, inside the event itchange
will refer to the checkbox, and withprop('checked')
you get the state of the element (true
if it is checked, orfalse
if it is not).The function would look like this:
Then wherever you need to get the value that is stored in
sessionStorage
do this:And you will have in
sessionStatus
the value that is stored.show
VER DEMO JSFIDDLE.
You can try to mark/unmark the element and with the button you will be able to see what value is stored in
sessionStorage
.