How to find a node in a range and then delete it?

I copy the range from the page and paste it back to another place, but this becomes problematic when it includes part of the page that I do not want to copy. Is there a way to remove nodes from a range by id? Or, if not, in case there are two nodes on the page with the same identifier, is there a way to identify one above the other (i.e., first indicating a unique parent identifier?)

Note: JavaScript only - I cannot use the library for this solution.

+3
source share
1 answer

If you are talking about DOM ranges, the solution is to split the range around the node that you want to exclude, giving you two ranges.

// Assuming you have a Range stored in a variable called range and
// a node you want to exclude in a variable called node
var newRange = range.cloneRange();
range.setEndBefore(node);
newRange.setStartAfter(node);

This will not work in IE <= 8, which has a completely different way of representing ranges.

+2
source

Source: https://habr.com/ru/post/1742098/


All Articles