0

I have a current sessionStorage in place. The .set method is working correctly.

The problem is the key it's not fixed. it's being created with the array of the first product I click.

like this: enter image description here

Like this, I can't have a key value to use the .get method in another page.

This is my JS.

$('.go-to-bag').click(function() {
  var productId = $(this).parent().attr('id');
  var productName = $(this).parent().attr('product-name');
  var productQty = $(this).prev().val();
  var productLWeight = $(this).parent().attr('product-weight');
  var productPesoBruto = $(this).parent().attr('product-pesobruto');
  var productPrice = $(this).parent().attr('product-price');
  var productPricesemiva = $(this).parent().attr('product-pricesemiva');

  var newList = new Object();

  if (sessionStorage.newShoppingList) {
    newShoppingList = JSON.parse(sessionStorage.getItem('newShoppingList'));
  } else {
    newShoppingList = []
  }

  newShoppingList.push({
    productId,
    productName,
    productQty,
    productPrice,
    productPricesemiva,
    productLWeight,
    productPesoBruto
  });

  sessionStorage.setItem('newShoppingList', JSON.stringify(newShoppingList));

  var retrievedObject = sessionStorage.getItem('newShoppingList');
  console.log('retrievedObject: ', JSON.parse(retrievedObject));

});

So, I can I get the key so i can use .get method and retrive the value inserted in another page?

Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0

The example that you've given us is correct. At this point I would review if that is truly the only place that you call .setItem.

Also, that last question makes me wonder -- are you intending to use localStorage instead of sessionStorage? See this link for the difference

W3Schools Web Storage Reference

Sam Clarke
  • 120
  • 1
  • 11
  • He's using EcmaScript-6 shorthand. `{a, b, c}` is short for `{a: a, b: b, c:c}` – Barmar Nov 06 '18 at 22:45
  • Oh, that's supported now? Great to know! Edited. – Sam Clarke Nov 06 '18 at 22:48
  • 1
    I suspect he really does want to use session storage. It's a shopping cart for a retail site, so he just wants it to last while the user is on the site. – Barmar Nov 06 '18 at 22:49
  • Well, let's put it, in a different way: I want to store the values from the product on this page: https://www.develop2you.com/equador/category.php?id=3&ids=26 and then display them on this page (right side column - like a shoplist): https://www.develop2you.com/equador/billing.php So, when i'm talking about sessionstorage.get I want to be able to take the values stored in the sessionStorage and display each product in that list. The reason for sessionstorage it's that i don't want to store any value when the user close the tab. – cristian Oliveira Nov 06 '18 at 22:50
  • Possibly. It's just this question that threw me off. `So, I can I get the key so i can use .get method and retrive the value inserted in another page?` If he can't retrieve the value from another page I thought he might not quite be getting the difference... **shrug** – Sam Clarke Nov 06 '18 at 22:50
  • when i use on the console: sessionStorage.get(newShoppingList); I get that newShoppingList it's not defined – cristian Oliveira Nov 06 '18 at 23:11
  • You're looking for `sessionStorage.get('newShoppingList');` there. – Sam Clarke Nov 07 '18 at 04:15