das_boot
das_boot

Reputation: 113

Checking for duplicate keys via HTML5 Local Storage

I wrote a function for a onClick button to save to localStorage. Everything worked fine, not a problem. I am trying to implement error checking to see if the key already exists, and then to prompt the user.

There was a previous question, that covered this, and I followed their answer, but I am still having no luck.

Here is the part in question:

  function save_dat_data() {


     //Gets value from scroll wheel
     var result = $('#i').scroller('getValue').join('');



     for (var i in localStorage) {
         if (localStorage[i] == result) {
             window.alert("This entry already exists");
             return;
         } else {
             localStorage.setItem(result, nameout);
             localStorage.saveServer
             //Resets the form
             document.getElementById('localStorageTest').reset();
         }
     }

 }

Upvotes: 0

Views: 1024

Answers (1)

Akhil Sekharan
Akhil Sekharan

Reputation: 12683

I guess you are checking for the key. not the value. Is it?

function save(result, nameout){
    for (var i in localStorage) {
         if (i == result) {
             window.alert("This entry already exists");
             return;
         } 
     }
     localStorage.setItem(result, nameout);
     localStorage.saveServer
     //Resets the form
     document.getElementById('localStorageTest').reset();
 }

Upvotes: 2

Related Questions