Home > other >  How to reload current page without losing added list items?
How to reload current page without losing added list items?

Time:12-24

I'm creating something similar to a to-do-list project, but whenever I refresh the page I lose all the added items, I've tried using:

`

  window.onbeforeunload = function () {
    localStorage.setItem("list", $("#listItem").val());
  };

  window.onload = function () {
    var name = localStorage.getItem("list");
    if (name !== null) $("#listItem").val("list");
  };

`

but still it doesn't work, I may have used it in the wrong place or wrong way. any help please? here is my full code:

HTML: `

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <title>To Do List</title>
  </head>
  <body>
    <section >
      <form >
        <h3>To Do List!</h3>

        <div >
          <input type="text" id="items-input" placeholder="e.g. eggs" />
          <input
            type="button"
            
            onclick="addItems()"
            value="Submit"
          />
        </div>

        <div >
          <ul id="faves"></ul>
        </div>
      </form>
    </section>

    <script src="main.js"></script>
  </body>
</html>

`

Javascript:

`

function addItems() {
  var li = document.createElement("LI");
  li.setAttribute("id", "listItem");

  var input = document.getElementById("items-input");
  li.innerHTML = input.value;
  input.value = "";
  document.getElementById("faves").appendChild(li);

  var deleteBtn = document.createElement("button");
  deleteBtn.classList.add("delete-btn");
  deleteBtn.innerHTML = "Delete";
  deleteBtn.type = "button";
  document.getElementById("faves").appendChild(deleteBtn);

  var hrzBreak = document.createElement("br");
  document.getElementById("faves").appendChild(hrzBreak);

  /*********/


  window.onbeforeunload = function () {
    localStorage.setItem("list", $("#listItem").val());
  };

  window.onload = function () {
    var name = localStorage.getItem("list");
    if (name !== null) $("#listItem").val("list");
  };
}

`

What am I doing wrong? I've included jQuery's CDN too, but still it doesn't work.

CodePudding user response:

    var texts = [];
function addItems() {
    var input = document.getElementById("items-input");
    createElement(input.value)
    input.value = "";
}

function createElement(value) {
    var li = document.createElement("LI");
    li.setAttribute("id", "listItem");
    li.innerHTML = value;

    document.getElementById("faves").appendChild(li);

    var deleteBtn = document.createElement("button");
    deleteBtn.classList.add("delete-btn");
    deleteBtn.innerHTML = "Delete";
    deleteBtn.type = "button";
    document.getElementById("faves").appendChild(deleteBtn);

    var hrzBreak = document.createElement("br");
    document.getElementById("faves").appendChild(hrzBreak);

    texts.push(value)

}

window.onbeforeunload = function () {
    // Store text array in storage
    localStorage.setItem("list", JSON.stringify(texts));
};

window.onload = function () {
    // get list grom storage
    var list = localStorage.getItem("list");
    if (list !== null) {
        list = JSON.parse(list)
        for (let index = 0; index < list.length; index  ) {
            const element = list[index];
            // create your dom element
            createElement(element)
        }
    }
};

Using an Array to manage the data flow. This will do the job but still a mess.

CodePudding user response:

Assuming that $("#listItem").val() will return the data you want, place below block outsite of the addItems() function

window.onbeforeunload = function () {
    localStorage.setItem("list", $("#listItem").val());
  };

window.onload = function () {
    var name = localStorage.getItem("list");
    if (name !== null) $("#listItem").val("list");
};

CodePudding user response:

Try adding event listeners once and outside of your function


window.onbeforeunload = function () {
  localStorage.setItem("list", $("#listItem").val());
};

window.onload = function () {
  var name = localStorage.getItem("list");
  if (name !== null) $("#listItem").val("list")  
};

function addItems() {
...
}

  • Related