I have the following which works fine for text inputs, textareas and selects with the class 'stored'.

$(function() {

    $.each($('.stored'), function() {
        if(localStorage[$(this).attr('name')]) {
            $(this).val(localStorage[$(this).attr('name')]);
        }
    });

    $('.stored').on('change', function() {
        localStorage[$(this).attr('name')] = $(this).val(), $(this).find('option:selected').val();
    });

});

To store radio buttons or checkboxes with the class 'stored' in localStorage I've tried to extend that on change function with several ways such as:

localStorage[$(this).attr('name')] = $(this).val(), $(this).find('option:selected').val(), this.checked.val();

But no can do!

Then I tried to seperate it with the following, but also nothing

if ($('.stored').is(':checked')) {
    localStorage[$(this).attr('name')] = $(this).val();
};

So how to do this exactly? I've created a pen with this all.
http://codepen.io/gentlemedia/pen/ORgQyr/

Thanks!

Recommended Answers

All 2 Replies

Thanks, Diafol! I know I go wrong with using name, because the radio buttons do have of course the same name, but I couldn't figure out what to use instead for the them. Your solution works well for me too, because I've tried long enough :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.