Daniel Ovári
Daniel Ovári

Reputation: 15

jQuery Form won't hide and function return undefined

I'm having a problem with one the buttons. It doens't work properly.
When I click "Add Run" button and then "Home" button almost nothing work. The form that is dynamically created doesn't hide and search input doesn't show, and function return undefined.

$("#home").on("click", function () {
  $("#search").show();
  $("#addForm").hide();
  $("#noPrevious").hide();
  complete()
  console.log(complete())
});

But on the other hand, some things work, they hide or show. For example, after refresh, when I click "All my Runs" it displays text, which will hide after clicking on "Home" button, and on top of that, search input field shows up. I don't understand this. Does anybody know what's wrong?

Thanks in advance.

Here is CodePen and GitHub. The problem is at the top of .js file. https://codepen.io/ovy1448/pen/gZrKOX
https://github.com/ovy1448/MyRun/blob/master/js/main.js

Upvotes: 0

Views: 112

Answers (1)

Lajos Arpad
Lajos Arpad

Reputation: 76464

Your problem is that you are not initializing your values properly. For example, in the case of bookmarks your code presumes that localStorage has a value with that key. However, that is not necessarily the case. You would need to have a default value for bookmarks. A helper function could be useful here:

function getBookmarks() {
    var bookmarks = localStorage.getItem("bookmarks");
    if (!bookmarks) bookmarks = [];
    return bookmarks;
}

and then replace all occurrences of localStorage.getItem("bookmarks") to getBookMarks() in your code, except the one in the function above. Pay attention to other initializations as well.

EDIT

If you want to parse JSON inside the function, you can do it like so:

function getBookmarks() {
    var bookmarks = localStorage.getItem("bookmarks");
    if (!bookmarks) return [];
    return JSON.parse(bookmarks);
}

Upvotes: 1

Related Questions