Skip to content
Advertisement

Ignore span stag in window selection to get the start and end index

I have a html tag which is

JavaScript

I am trying to get the start and end index from the selected text. When I select third I get start and end index as 34 39 But I expect 27 32

I tried the below approach

JavaScript

Is there any way where I can ignore the span element with ignore class.

Advertisement

Answer

Store the initial HTML, then remove all elements having the .ignore class:

JavaScript

After getting the range, restore the original HTML:

JavaScript

Snippet

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement