title: localStorage: condition to prevent storing duplicated values in localStorage
body: <p>When clicking a li element, I want to store its text as a localStorage value only once. If it exists in localStorage, a second click must have no effect (just an alert).</p>
  
  <p>To check if the string exists I'm doing an if inside a for loop, but I'm doing something wrong. (fiddle line 26). <strong>My question is if there is a way to make this condition work.</strong></p>
  
  <p>I can not use the colors as keys, since my real script uses large strings instead of colors. Adding a class is not the way I want to solve it.</p>
  
  <p>Thanks</p>
  
  <p><a href="http://jsfiddle.net/SXjtd/3/" rel="nofollow">http://jsfiddle.net/SXjtd/3/</a></p>
  
  <pre><code>// Each color must be stored with localStorage once with a click.
  // The second click must have no effect, just run an alert.
  // LINE 26: the problem is while checking if the value exists
  // Colors are used for this example, the real js uses long strings, that's why I can not use the colors as keys.
  
  localStorage.clear();
  
  // define a value that will be used for increment
  if (localStorage.getItem('current_id') === null) {
     localStorage.setItem('current_id', 0);
  } 
  
  $(document).on('click', 'li', function() {
    var dl = $('dl');
    var current_color = $(this).text();
  
     // each click generates a new key
     current_key = 'color_id_' + (parseInt(localStorage.getItem('current_id')) + 1);
    localStorage.setItem('current_id', (parseInt(localStorage.getItem('current_id')) + 1));
  
      $('&lt;dt&gt;' + current_key + '&lt;/dt&gt;&lt;dd&gt;' + current_color + '&lt;/dd&gt;').appendTo(dl);
  
    // THE PROBLEM IS HERE
    // I want to know how to check if a value exists in localStorage
    // if this value doesn't exist, it is set in localStorage with a click
     for (var i = 0, len = localStorage.length; i &lt; len; i++) {
      if (localStorage.getItem('color_id_' + i) == current_color) {
        alert('Color exists in localStorage.');
      } else {
        alert('New Color added to localStorage');
        localStorage.setItem(current_id, current_color);
      }
    }   
  
  
  });
  </code></pre>