jQuery Auto Save Form Data in LocalStorage

Automatically save form data in local storage so that it persists on page refresh.
$('input, textarea').on('keyup', function() {
    var id = $(this).attr('id');
    var value = $(this).val();
    localStorage.setItem(id, value);
});

// Retrieve saved data
$('input, textarea').each(function() {
    var id = $(this).attr('id');
    if (localStorage.getItem(id)) {
        $(this).val(localStorage.getItem(id));
    }
});