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

  • SEARCH
  • Home
  • 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 7680649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:09:48+00:00 2026-05-31T18:09:48+00:00

I use getSelection() (javascript) to get a string onmouseup. Is it possible to get

  • 0

I use getSelection() (javascript) to get a string onmouseup.

Is it possible to get the div containing the selection? or the parent element containing the selection if it is not a div?

The code is similar to this JSFiddle Example and I want to be able to identify if the selection was inside first div or second div.

Thanks

  • 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-31T18:09:50+00:00Added an answer on May 31, 2026 at 6:09 pm

    Your selection object contains a range.

    var my_selection = window.getSelection();
    var my_range  = my_selection.getRangeAt(0);
    

    Sounds like you want commonAncestorContainer, which is most specific element that contains both your start and end elements.

    Some other useful methods on the range…
    startContainer and startOffest will get you the element and position in the element where the range begins. Similarly, endContainer and endOffset.

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

Sidebar

Related Questions

I'm trying to use window.getSelection to get a string but it's returning an object.
I can use the following code to get selected text: text=window.getSelection(); /// for Firefox
I use this simple Javascript code to retrieve text that was selected on a
I was in Unity 1.2, use the following code: UnityConfigurationSection UnitySection = (UnityConfigurationSection)ConfigurationManager.GetSection(Unity); Dictionary<string,
how can i use this javascript code on a webpage, who is inside an
I'm trying to use the document.getSelection to select text I have entered in an
Im using scala Map#get function, and for every accurate query it returns as Some[String]
I've written a bookmarklet to look a word up in a Chinese dictionary: javascript:Qr=document.getSelection();if(!Qr){void(Qr=prompt('%E8%AF%8D%E8%AF%AD',''))};if(Qr)(function(){window.open('http://nciku.com/search/all/'+Qr);})();
I use the following code to set text to an AutoCompleteTextView field. But I
I would like to position a floating div element in an iframe with contentEditable

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.