top | item 38298010

(no title)

jypepin | 2 years ago

insane, I never thought that `toList.appendChild(item.closest('li'));` would MOVE the item. You learn something every day

discuss

order

chrismorgan|2 years ago

If you add a DOM node somewhere, it’s first removed from where it was because it can only exist in one place. You need to clone the node if that’s not what you want.

Incidentally, here’s a briefer spelling of that function (skipping the superfluous Array.from(), using a for loop instead of forEach, and using .append() instead of .appendChild(), cumulatively reducing 8 years of browser support to 5½+ years, which is no meaningful difference; and although I’ve declared Array.from() superfluous, note that this is only the case because querySelectorAll returns a non-live NodeList—you couldn’t do this with childNodes since it’d be being mutated during iteration so you’d miss half the items due to how it all works):

  function moveSelectedItems(fromList, toList) {
      for (const item of fromList.querySelectorAll('input[type="checkbox"]:checked')) {
          item.checked = false; // Uncheck the item
          toList.append(item.closest('li')); // Move the entire list item
      }
  }

chatmasta|2 years ago

Yeah, to the extent "I need to lie down," it's actually due to the features I didn't even know existed. In that followup with the accessibility corrections, I had no idea you could even do those things...