how to create a "Store list"?

Asked

Viewed 149 times

0

Each time the user clicked on the button, I would like you to save the information to a localStorage different and tried to do with for, and increment 1 every time you click the button, like this:

for(int i=0; i<10; i++) {
    localStorage.setItem('modeloProduto'+i, $('.divProdCarrinho').html());
    var teste = localStorage.getItem('modeloProduto');
}

then I would be modelProduct, modelProduct, etc..

It is possible?

  • yes it is possible, but not the way you are doing it. Recovery also cannot be done like this, what is the reason for it ... a Shopping Cart?

  • Yes, that’s the point.

2 answers

0

Yes, I also do this in an application, but you need to save the index in Local Storage:

let i = localStorage.getItem('index') || 0;
for(; i<10; i++) {
    localStorage.setItem('modeloProduto'+i, $('.divProdCarrinho').html());
}
localStorage.setItem('index', i);

At the beginning of the application you should take the Storage value and update it when changing the variable value

0

Since localstotage is a Key->Value type Log, it is not possible to create an array in it, but it is possible to transform the text to JSON string and save it. Then just parse that text. As follows:

var array = [];

function adicionar(dados) {

    var itemList = localStorage.getItem('nomeDoItem')
        ? JSON.parse(localStorage.getItem('nomeDoItem'))
        : [];

    itemList.push(dados);

    localStorage.setItem('nomeDoItem', JSON.stringfy(itemList);

}

Every time parse is performed, the array will have the items that were added in the push

Browser other questions tagged

You are not signed in. Login or sign up in order to post.