4this
4this

Reputation: 759

jquery set a key value

Well I've got the next code in a javascript file -

$(document).ready(function () {
    localStorage.clear();
    var listOfNames = [{
        name: "Dan",
        selected = "false"
    }, {
        name: "Ben",
        selected: "false"
    }];
    localStorage.setItem("namesList", JSON.stringify(listOfNames));
    //}
});

And what i would like to happen is simple - i want by a click of a button (one says dan and the other one ben) the selected key will turn into "true" and will be save. so than i would be able to present the selected name in another html page.

I tried to do as fallows - in the script of the html page -

var dataNames = localStorage.getItem("namesList");
var names = JSON.parse(dataNames);

function select(i) {
    names[i].selected = "true";
}

But it turns out that the value changed but from some reason , when i try to a access to the java script file (with the names) from another html page it says the the selected key is still false.

so what i am trying to ask is how could i save the value for a key, and make it "remember" the change so it can be noticed when reading the data from another html page.

Thanks in advance for any kind of help

Upvotes: 0

Views: 582

Answers (1)

dc5
dc5

Reputation: 12441

You aren't saving the updated object to localStorage.

Change:

function select(i) {
    names[i].selected = "true";
}

to:

function select(i) {
    names[i].selected = "true";
    localStorage.setItem("namesList", JSON.stringify(names));
}

Upvotes: 2

Related Questions