mutation-summary icon indicating copy to clipboard operation
mutation-summary copied to clipboard

get childNodes index of removed nodes

Open marcelklehr opened this issue 10 years ago • 2 comments
trafficstars

Hey,

over at http://github.com/marcelklehr/dom-ot it would be nice to have a way to find out the index a removed dom node had in its parent's childNodes NodeMap. Currently, there's only oldPreviousSibling, however, if that has also been removed, there's no way to find out which index it had.

marcelklehr avatar Jun 23 '15 20:06 marcelklehr

It would be useful to see index information on nodes in a summary.

div.number-1 [index: 0]
div.number-2 [index: 1] (removed)
div.number-3 [index: 2] (removed)
div.number-4 [index: 1] (added)

That would almost make oldPreviousSibling obsolete.

Kequc avatar Jan 30 '16 05:01 Kequc

The way MutationObserver works, from which MutationSummary is built. The MutationObserver returns individual removal records but sometimes many at the same time.

var divs = document.querySelectorAll('div');
divs[2].remove();
divs[1].remove();

You'll receive an array containing both removals from the MutationObserver. divs[2]'s mutation previousElementSibling would be div[0] and div[1]'s mutation previousElementSibling would also be div[0]. Both the same.

You could then do some maths to figure out what the indexes were, but not necessarily the order they were in before removal. MutationSummary though, does add a little bit more complexity. A lookup of the old positioning should be possible.

Kequc avatar Jan 30 '16 07:01 Kequc