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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:28:26+00:00 2026-06-11T21:28:26+00:00

I am trying to get the selected texts from the user(the highlighted text that

  • 0

I am trying to get the selected texts from the user(the highlighted text that user highlights).

I have the following:

function getSelectedTexts(){
  var t = '';
  if(window.getSelection){
    t = window.getSelection();
    console.log('1');
  }else if(document.getSelection){
    t = document.getSelection();
    console.log('2');
  }else if(document.selection){
      console.log('3');
    t = document.selection.createRange().text;
  }
    return t;
   }


 $('.text_speech').live('click',function(e){
      e.preventDefault();
      var textTest='';

      textTest=getSelectedTexts();
      console.log(textTest);

  })

My console returns

1

>Selection   <------object
 anchorNode: Text
 anchorOffset: 2
 baseNode: Text
 baseOffset: 2
 extentNode: Text
 extentOffset: 1
 focusNode: Text
 focusOffset: 1
 isCollapsed: false
 rangeCount: 1
 type: "Range"
 __proto__: Selection

I am not sure how to get the selected texts. Anyone can help me about it? Thanks a lot!

  • 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-06-11T21:28:27+00:00Added an answer on June 11, 2026 at 9:28 pm

    window.getSelection() returns a Selection object.

    If you want to get the selected text without thinking more, you should do:

     window.getSelection().toString()
    

    But if your selection can be more complex, you should read the MDN documentation.

    For example the user might select text with multiple tags:

     <div class="text_speech">
       <p>First paragraph</p>
       <p>Second paragraph <strong>big</strong></p>
     </div>
    

    If the user select all the sentence ‘paragraph Second paragraph big‘ then the easy way won’t work.

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

Sidebar

Related Questions

I am trying to get the value user selected from search prompt and pass
I am trying to get the anchorOffset of text selected by the user in
I am trying to get the selected option from a dropdown and populate another
I am trying to get value of selected item from dropdown menu via val()
I'm trying to get the DOM node of the current selected text. I'm building
I'm trying to use a simple PHP mail() function to create user feedback from
Goal: I want to get the text from the selected element and then create
I'm trying to build a script that will add the currently selected text to
I am trying to get the selected items string out of a Spinner .
im trying to get the currently selected option in a dropdown menu with javascript.

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.