0

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!

2
Contributors
2
Replies
16
Views
10 Months
Discussion Span
Last Post by gentlemedia
0

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 :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.