Search code examples
javascripthelper

when the function works the else statement is the one that executed


Please Guys some Help Really i stuck here why the else statement works rather than if statement

function add() {
  if (input.value !== "") {

    localStorage.setItem("name", input.value);

    results.innerHTML = `localStorage item <span class='green'>${input.value}</span> been added`;

    input.value = '';
  }
};

when the add() function add a value from the input i want to check if this value exists in the localStorage but the else statement that is the one that works

function check() {
  if (input.value !== "") {

    if (localStorage.getItem(input.value)) {

        results.innerHTML = `Found item in Local Storage With Name ${input.value}`;

    } else {
        results.innerHTML = `No Local Storage with Name <span class='green'>${input.value}</span>`;

    }
  }
}

Solution

  • You need to get the item by its name. localStorage.getItem(input.value) should be localStorage.getItem("name")