Persisting multiple checkbox states in session Storage (using jQuery)

Standard

The problem:

An existing ASP.NET MVC page allowed the user to apply additional an additional filter a to jQuery DataTable with a bunch of checkboxes in a modal/popup. These were being forgotten each time the user followed one of the links in the table and then returned.

The filtering took values directly from the checkboxes.

The state of the checkboxes needed to be saved when the modal was closed, then reloaded when they returned to the page.

The solution:

A quick fix using session storage on the client.

A brief explanation of Window.sessionStorage

It gives you access to an object on which to store key-value pairs, on the client side for the duration a window (or tab) is open.

Window.localStorage is also available, differing in that it does not expire on closing of the window.

As this was an internal website with all users on IE11 and there was no server-side need for the data, cookies were deemed unnecessary.

Key bits

To save the state of the check-boxes to session storage as JSON array:

To reload the state of the check-boxes:

Side note

The jQuery.inArray()> function behaves in a similar way to array.indexOf i.e.

JavaScript’s Array indexOf() method returns the index of the specified item, which is -1 if  not found. Similarly, jQuery’s inArray returns -1 for not found or otherwise the index (first only) where the item is found.

The consequence being, we need to avoid the == or !=, as doing so will result in a 0 being evaluated as false. So remember to use !==. Not so important in the example above as we are checking for -1 only.

JSFiddle

CSS for checkboxes borrowed from aahz’s Bootsnip.

Leave a Reply