Skip to content Skip to sidebar Skip to footer

Incrementing Key Value To Save Data In Localstorage

I need to increment key value when I'm saving data to localstorage because when I click button key is same for all records and record just update,doesn't creating new. I know I can

Solution 1:

You need declare the variable before the function, to save the value and add 1+.

    (function() {

  window.onload = function() {
    var key = 0; //move var key to here
    document.getElementById('buttonCreate').onclick = function() {
      var topicValue = document.getElementById("create-topic").value;
      var statusValue = document.getElementById("create-status").value;
      var descriptionValue = document.getElementById("create-description").value;
      // var key = 0; remove these line
      var storage = new Storage();
      var ticket = {
        topic: topicValue,
        status: statusValue,
        description: descriptionValue
      };
      storage.set(key, ticket);
      return (function() {
        key++;
        return key;

      }());
    }
  }

})();

Post a Comment for "Incrementing Key Value To Save Data In Localstorage"