September 11, 20233 yr I have a text area that contains text. After a button is clicked, this text is put into a div with spans for each word for styling purposes. For example, the text could say "Hello World!" This will be split into 3 spans, one for each word and one for the space in between the words. Now, if a user selects part of the text in the text area I need to detect which spans contain the string that has been selected. So the user could select from the 'o' from 'Hello' up to the 'o' from 'World'. In this example, spans 1, 2 and 3 have been selected. How can I detect this? You'd think this would be simple but I'm struggling to find a way to do it. Here are some variables I've set up: text - contains all of the text from all of the spans, so the same as the text area value words - an array containing all the text split up into words start - the index of the start of the selection within the text end - the index of the end of the selection within the text selection - a substring between start and end containing the selection selectionSplit - an array containing the selection split up by spaces beforeSelection - contains the string that appears before the start of the selection, up to but not including the selection textToSelectionEnd - contains the string before and including the selection Taking these variables into account, is there a way I can detect which spans contain the selection?
Create an account or sign in to comment