A remove button hides one list row and announces the result while guarding against a missing row.

Program

Removing an item should update the list and say what changed. A small row lookup keeps the action deterministic and safe.

remove_item_status.html
Visuals: captured from real browser rendering
<ul id="cart">
  <li id="item-1">Notebook <button data-remove="item-1">Remove</button></li>
  <li id="item-2">Pencil</li>
</ul>
<p id="remove-status" role="status" data-tone="idle">No items removed.</p>
<style>
  #remove-status[data-tone="done"] { background: #dcfce7; padding: 8px; }
</style>
<script>
  const removeStatus = document.querySelector("#remove-status");
  document.querySelectorAll("[data-remove]").forEach(button => {
    button.addEventListener("click", () => {
      const row = document.getElementById(button.dataset.remove);
      if (!row) return;
      row.hidden = true;
      removeStatus.dataset.tone = "done";
      removeStatus.textContent = "Notebook removed. 1 item left.";
    });
  });
</script>
  1. Store the row id on the button.

    The button carries the id of the row it removes.
    The button carries the id of the row it removes.
  2. Add a removal status line.

    The list has a polite place to report changes.
    The list has a polite place to report changes.
  3. Style the completed remove state.

    A successful remove gets a visible confirmation style.
    A successful remove gets a visible confirmation style.
  4. Listen for remove clicks.

    Clicking Remove starts the row update path.
    Clicking Remove starts the row update path.
  5. Find the target row.

    The button data points to the list item that will change.
    The button data points to the list item that will change.
  6. Guard against a missing row.

    The handler exits if the target row does not exist.
    The handler exits if the target row does not exist.
  7. Hide the row and report it.

    The row disappears and the status message confirms the removal.
    The row disappears and the status message confirms the removal.
data attribute A data attribute can store the id of the row an action changes.
hidden hidden removes the row from the rendered list.
role status role="status" announces the result of the remove action.