Ollie Clark
Ollie Clark

Reputation: 141

Removing multiple rows of data from local storage

currently I am using the following code to remove a row of data within a table from HTML5 local storage:

$("#deletedata").click(function() {
    var itemId = $("input:checked").parent().parent().attr('id');
    localStorage.removeItem(itemId);
});

Basically I have a checkbox within each row, when this is checked and the 'delete selected' button is pressed this bit of code targets the parent row holding the data. It works fine, however it doesn't work if multiple rows are selected - it will only remove the first row it finds from localstorage. I need the 'delete selected' button to remove ALL selected rows.

Can anyone help with this? Thanks!

Upvotes: 1

Views: 1123

Answers (1)

Wilfredo P
Wilfredo P

Reputation: 4076

Try using .each() to get all input checked, and delete the item in the local storage:

$("#deletedata").click(function() {
    $("input:checked").each(function(){
    var itemId = $(this).parent().parent().attr('id');
    localStorage.removeItem(itemId);
   });
});

In your code only get the first and delete.

Upvotes: 1

Related Questions