-1

I have an array containing four element located in local storage.

   function rm(t)
   {   
           //   var l = localStorage.getItem("subje");
          // hide element from html page
          document.getElementById(t).style.display = 'none';
         var c = localStorage.getItem("class");
         var item = c+""+t;  // c is int and t is string
         var subj = localStorage.getItem("std-sub");
          var x = subj.indexOf(item);
           /*
          subj.splice(x,1);
           localStorage.setItem("final", subj);
            var k = localStorage.getItem("final");*/
           document.getElementById('show').innerHTML = x;
           }

when I executing function the value of x is showing 29,17,7,0. but my given array have only four element. why?????

Rauz Saul
  • 13
  • 4

1 Answers1

1

getItem always returns a string (or null). Therefore subj.indexOf(item); will get the position of the substring item in the string subj. You should encode and decode the array as JSON and store that in local storage instead.

See Storing Objects in HTML5 localStorage

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143