Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get childNodes index of removed nodes #15

Open
marcelklehr opened this issue Jun 23, 2015 · 2 comments
Open

get childNodes index of removed nodes #15

marcelklehr opened this issue Jun 23, 2015 · 2 comments

Comments

@marcelklehr
Copy link

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.

@Kequc
Copy link

Kequc commented Jan 30, 2016

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
Copy link

Kequc commented Jan 30, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants