Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Local Storage Project

Jesse Dispoto
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jesse Dispoto
Front End Web Development Techdegree Graduate 14,538 Points

Why wont this work for me?

        <script type="text/javascript">
        'use strict';
      function supportsLocalStorage() {
        try {
      return "localStorage" in window && window["localStorage"] !== null 
        } catch(e) {
         return false;
      }
       }

        function getRecentSearches() {

      var searches = localStorage.getItem("recentSearches");
      if(searches){
        return JSON.parse(searches);

      } else {
        return [];
      }

       }

       function saveSearchString(str) {
      var searches = getRecentSearches();
      if (!str || searches.indexOf(str) > -1) {
        return false;
      }
      searches.push(str);
      localStorage.setItem("recentSearches", JSON.stringify(searches));
      return true; 
    }

    function removeSearches() {
      localStorage.removeItem("recentSearches");
    }

    // Create an li, given string contents, append to the supplied ul
    function appendListItem(listElement, string) {
      var listItemElement = document.createElement('LI');
      listItemElement.innerHTML = string;
      listElement.appendChild(listItemElement);
    }

    // Empty the contents of an element (ul)
    function clearList(listElement) {
      listElement.innerHTML = '';
    }

    window.onload = function() {
      if (supportsLocalStorage()) {
      var searchForm = document.getElementById('searchForm');
      var searchBar = document.getElementById('searchBar');
      var recentSearchList = document.getElementById('recentSearchList');
      var clearButton = document.getElementById('clearStorage');

      // Initialize display list
      var recentSearches = getRecentSearches();
      recentSearches.forEach(function(searchString) {
        appendListItem(recentSearchList,searchString);
      });

      // Set event handlers
      searchForm.addEventListener('submit', function(event) {
        event.preventDefault();
        var searchString = searchBar.value;
        if (saveSearchString(searchString)) {
          appendListItem(recentSearchList, searchString);
        }
      });

      clearButton.addEventListener('click', function(event) {
        removeSearches();
        clearList(recentSearchList);
      });
      }  
    };
     </script>

Can't figure out why this isn't working for me. Followed the video numerous times. Still not saving searches. I added (event).preventDefault() at the top of the submit event handler to stop the page from reloading every time, and it is still not working. Anyone know where my problem might be?

1 Answer

I pasted your code into the video workspace and it worked (other than you're missing a semicolon after null). Can you post a snapshot of your workspace?