Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3481702
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:28:35+00:00 2026-05-18T10:28:35+00:00

I am able to grab the text that a user has selected on a

  • 0

I am able to grab the text that a user has selected on a web page,
using this code:

function getSelected() {
  var userSelection;
  if (window.getSelection) {
      selection = window.getSelection();
  } else if (document.selection) {
      selection = document.selection.createRange();
  }

}

is it posible for me to get the words around the
selected word.

Take these sentences for example: “If you need to
framglubble the zartbox, then you should buy the red widget.
Otherwise you can buy the blue widget and save some money.”

My code will tell me if the person has selected the word “widget”.
But I’d like to know if the selection is after “red” or “blue”. Is
this possible? I’ve been scouring the Internet for some advice, and
I’m having trouble finding an answer.

thank you for your help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:28:36+00:00Added an answer on May 18, 2026 at 10:28 am

    I have written quick script that can identify the part before selection and after selection inside the same DIV element.

    However if the same DIV contains the same word more than one time and you select only that word, the current code I wrote can’t identify if it’s the first or second selected word so bottom line it will not answer your needs.

    Anyway, you can see/copy/test the code here: http://jsfiddle.net/kvHxJ/ just select something and see the alert that appears.

    If it’s enough for your needs after all then great, accept this answer and move on… otherwise I need to know: can we assume the user will select whole words only, one word only? If the answer is yes I do have idea how to go around this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an application that needs to be able to grab questions
I have two classes. Looking to be able to grab a variable that is
I want to be able to capture the exception that is thrown when a
Has anyone been able to get an NHibernate-based project up and running on a
I want to make a site where there user can basically navigate the web
I am using DD_roundies to generate rounded corners in Internet Explorer. The pages that
Application able to record error in OnError, but we are not able to do
anyone able to tell me how often a materialized view is set to refresh
You should be able to create a generic form: public partial class MyGenericForm<T> :
I'm able to connect to and read an excel file no problem. But when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.