Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.4 kB
4
Indexable
Never
let data = JSON.parse(localStorage.getItem("data")) || [];

let todoInput = document.querySelector(".todo-input");
let select = document.querySelector("#select");
let addTodo = document.querySelector(".add-todo");
let inputForm = document.querySelector(".input-form");

let tbody = document.querySelector(".tbody");

function createRow(obj) {
  let tr = document.createElement("tr");
  let td1 = document.createElement("td");
  let td2 = document.createElement("td");
  let td3 = document.createElement("td");
  let td4 = document.createElement("td");
  let btnArchive = document.createElement("button");

  btnArchive.className = "btn-archive";

  td1.innerText = obj.task;
  td2.innerText = obj.priorirty;
  td3.innerText = `Pending`;
  btnArchive.innerText = `Archive`;

  td4.append(btnArchive);
  tr.append(td1, td2, td3, td4);
  //   tbody.append(tr);

  let arr = JSON.parse(localStorage.getItem("data")) || [];
  arr.push(obj);
  localStorage.setItem("data", JSON.stringify(arr));
  return tr;
}

inputForm.addEventListener("submit", (e) => {
  e.preventDefault();
  let dataObj = {
    task: todoInput.value,
    priorirty: select.value,
  };

  let tr1 = createRow(dataObj);
  tbody.append(tr1);
});

// function appendData(obj) {
//   obj.forEach((elem) => {
//     let tr = createRow(elem);
//     tbody.append(tr);
//   });
// }

// appendData(data);
Leave a Comment