HyperX
HyperX

Reputation: 39

Delete from localStorage

I am trying to use local storage for my little table where i just want to save few things here is my current code:

    $("#dodaj").click(function()
    {


        var str = "<tr onclick='Oznaci(this)'><td>"+stevilka+"</td><td>"+naslov+"</td><td>"+vrsta+"</td><td>"+nujnost+"</td><td>"+datum+"</td></tr>";
        $("#tabela").append(str);

        localStorage.podatki = localStorage.podatki + str;
    });

    $("#odstrani").click(function()
    {
        {
            $(".rdeca").remove();   
            localStorage.removeItem(".rdeca");      
        }
    });




function Oznaci(vrstica)
{
    $(vrstica).css({"background-color":"#FF0000"});
    $(vrstica).attr("class","rdeca");
}

I sucessfully add to the localStorage, but when i call localStorage.removeItem(".rdeca"); it does not remove it. I am probably removing the wrong thing (.rdeca). As i should probably remove the string combined like str from it, but i dont know how i can manage to do thath. THanks for your help

Upvotes: 2

Views: 3200

Answers (2)

Wayne
Wayne

Reputation: 60424

The only assignment to localStorage that's shown in your code is this:

localStorage.podatki = localStorage.podatki + str;

The name of the item you stored is podatki so you need to remove it using that name:

localStorage.removeItem("podatki")

If your goal is to remove just a part of the value stored under that name, then you'll need to perform that operation manually. That is, you'll need to read the property, modify it as needed, and then write back to it.

Upvotes: 8

epascarello
epascarello

Reputation: 207557

The item in localstorage has no jQuery methods to update it.

You need to manually pull the stuff out of localstorage, delete the node, and reinsert it.

Upvotes: 1

Related Questions