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 206757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:40:28+00:00 2026-05-11T17:40:28+00:00

After highlighting text, I would like to obtain the paragraph in which the selected

  • 0

After highlighting text, I would like to obtain the paragraph in which the selected text resides.

var select = window._content.document.getSelection();

Any pointers please?

  • 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-11T17:40:29+00:00Added an answer on May 11, 2026 at 5:40 pm

    This is actually rather hard to do because you have to consider six cases:

    1. The selection is not within a paragraph (easy);
    2. The entire selection is within one paragraph (easy);
    3. The entire selection crosses one or more sibling paragraphs (harder);
    4. The selection starts or ends in an element not within a paragraph (harder);
    5. The paragraphs spanned are at different levels eg one is within a list item while two others are siblings of the list (even harder); and
    6. Some combination of the above.

    So firstly you have to decide how complete you want the solution to be. I’ll only cover the simplest cases of (1) and (2).

    function getSelectedParagraphText() {
      if (window.getSelection) {
          selection = window.getSelection();
      } else if (document.selection) {
          selection = document.selection.createRange();
      }
      var parent = selection.anchorNode;
      while (parent != null && parent.localName != "P") {
        parent = parent.parentNode;
      }
      if (parent == null) {
        return "";
      } else {
        return parent.innerText || parent.textContent;
      }
    }
    

    Note: If you’re after tags too replace textContent with innerHTML.

    Edit: Better version put in, including better browser compatibility.

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

Sidebar

Related Questions

I have the following text for which I would like to add a <br>
I am after a JavaScript Rich Text Editor that supports highlighting words and triggering
I would like to write a simple plain text editor including simple markdown instructions
I am going to write a text editor in Qt which can provide highlighting/code
After installing Dreamweaver CS6, the syntax highlighting of razor in visual studio 2010 removed
After discovering on superuser that there is no current way to add syntax highlighting
I have an XML document, want to do syntax highlighting. The code uses XPathDocument
After searching for a long time for a simple way of changing the text
UPDATE The second after I posted this question, thanks to the syntax highlighting of
I'm writing a syntax highlighting text editor in Java and I've run into a

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.