javascriptarraysfunctionrowreusability

How to make my Array to be empty again and reusable for my Edit Text Function


Newbie here.. I was making an expense note app(just a noob app). I have this button function on which when I select one table row.. It will be deleted and the table row input text value will return to the input bar text area(name, date, amount, remarks). I was happy when it work.

But it only work once.

Because when I select different table row data. It will be deleted but the same "first input data value" will always return to the input text bar..

It seems the first table data are being saved in the empty array function that can be reuse again. What I am hoping for is when I use the empty array function it will be empty again to be use in another different table row data.

I am using array methods but failed or my If statement is wrong. Hopefully you can answer this :) thanks

document
  .getElementById("editSelection")
  .addEventListener("click", editSelection);

function editSelection() {
  var editName = [];
  var editDate = [];
  var editAmount = [];
  var editRemarks = [];

  let selectedRows = document.getElementsByClassName("selected-row ");
  while (selectedRows.length > 0) {
    editName.push(cell0.innerText);
    editDate.push(cell1.innerText);
    editAmount.push(cell2.innerText);
    editRemarks.push(cell3.innerText);

    selectedRows[0].parentNode.removeChild(selectedRows[0]);

    var name = document.getElementById("inputName");
    name.value = editName.join("\n");

    var date = document.getElementById("inputDate");
    date.value = editDate.join("\n");

    var amount = document.getElementById("inputAmount");
    amount.value = editAmount.join("\n");

    var remarks = document.getElementById("inputRemarks");
    remarks.value = editRemarks.join("\n");
  }

  if (name || date || amount || remarks) {
    editName.splice([0], editName.length);
    editDate.splice([0], editDate.length);
    editAmount.splice([0], editAmount.length);
    editRemarks.splice([0], editRemarks.length);
  }
}


Solution

  • If you define an empty array

    var editName = [];
    

    and fill it with values you can empty it again with

    editName = [];