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 trialJesse Dispoto
Front End Web Development Techdegree Graduate 14,538 PointsWhy 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
KRIS NIKOLAISEN
54,972 PointsI 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?
Jesse Dispoto
Front End Web Development Techdegree Graduate 14,538 PointsJesse Dispoto
Front End Web Development Techdegree Graduate 14,538 PointsWeird, here is a snapshot of my workspace https://w.trhou.se/o00bgoy1t0