When selecting texts in HTML documents, you can start from one DOM element to another element, possibly passing through several other elements along the way. Using the DOM API, you can get a range of selections, selected texts, and even the parent of all selected DOM elements (using commonAncestorContainer or parentElement () based on the browser you use). However, I do not know that I can specify all elements containing elements of selected texts, except for getting a single parent element that contains all of them. Using the parent element and moving the child nodes will not do this, as there may be other siblings that are not selected inside this parent.
So, is there any way that I can get all of these elements containing the selected texts. I am mainly interested in getting the elements of the block (p, h1, h2, h3, ... etc.), but I believe that if there is a way to get all the elements, then I can go through them and filter them so that get what i want. I welcome any ideas and suggestions.
Thanks.
javascript dom html range selection
cria
source share