Question

I am trying to get the amount of objects saved to a session storage element.

I need to find out how many tickets have been saved into session storage; tickets.

So far the code I have is;

$('#price').html(sessionStorage['tickets']);

I have tried sessionStorage.length['tickets']); and a few other variations to no avail. I am aware this is a simple request but have trawled the internet and haven't gotten far.

This function created sessionStorage tickets by passing a class into it.

function confirm(){
    var str="";
    if(!sessionStorage['user']){
          alert('You Must First Sign In or Register!');
         } else{
     if($(".tickets").length>0){
         $(".tickets").each(function(){
                str += this.id+" "; 
                sessionStorage.setItem('tickets', str);


               });
         } else {
          alert("You have not sellected any seats.");
         }
     //alert(""+sessionStorage['tickets']);
       window.location="index.php";
         }

}
Was it helpful?

Solution

If you want get the number of tickets you can use this code:

sessionStorage.getItem ('tickets').split (' ').lenght

Regards, Kevin

OTHER TIPS

from what I can tell you're storing a space delimited list of element ids in session storage under the key 'tickets'.

to get the number of items you could do something like this (assuming your ids do not have spaces in them):

var numberOfTickets = sessionStorage.getItem('tickets').split(' ').length;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top