Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to detect which span has been selected?

Featured Replies

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.