Recover data from the localStorage in its given input!

0

I have a code that dynamically registers the input data in the localStorage.

I am trying to recover the data registered in their respective inputs, but without success.

This is the code to recover the data, but only retrieve the first data:

$("#exibir").click(function(){
        for (var i = 0; i < localStorage.length; i++){  
        var inputs = $('input[type="text"]');
      inputs.val(localStorage.getItem(localStorage.key(i)));
  }
})

JSFIDDLE

Thank you in advance!

    
asked by anonymous 24.02.2017 / 19:34

1 answer

0

Your problem is not with localStorage , this part of the code is perfect. The problem is with your inputs variable. Because $('input[type="text"]') returns an array with all <inputs> of type text , and therefore you are calling the .val() method on an array of elements.

[ <input>, <input>, <input> ].val("9001");  // Não vai funcionar!

You need to iterate through all the elements in your array inputs and, for each, call .val() .

    
25.02.2017 / 05:13