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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:28:27+00:00 2026-05-27T05:28:27+00:00

Looking for a method to target selected text and perform an action on it

  • 0

Looking for a method to target selected text and perform an action on it in Javascript. What kind of methods should I be using? Is this a job for jQuery? Thanks so much!

EDIT: Earlier answers regarded targeting a CSS class. I’m looking for clicking and highlighting/selecting text and then acting on that in JS. Thanks!

EDIT 2: I’ve been asked for an example of this function. Here’s one but the code has poor reviews. Let me know what you think.

  • 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-27T05:28:28+00:00Added an answer on May 27, 2026 at 5:28 am

    It is quite difficult to write a cross browser, generic “get selected text” function. If you can limit your requirements to say only text selected in a page, then life is simpler.

    But if you want to be able to get text selections from anywhere (inside form controls, button labels, general text), then life is tough.

    Here’s a function I wrote some time ago, it worked well enough for the application it was used in:

    /* 
     *  This function returns the selected text in a document.
     *  If no text selected, returns an empty string.
     *
     *  Call on one of the following events: 
     *
     *     mouseup - most appropriate event
     *               for selection by mousedown, drag to select, mouseup
     *               may select only whitespace
     *
     *    dblclick - not as appropriate as mouseup
     *               for selection of word by double click
     *               may select only whitespace
     *
     *  Note that text can be selected in ways that do not dispatch
     *  an event, e.g. selecting all the text in the document using:
     *     ctrl + a
     *     context menu -> Select All
     *     edit menu -> Select All
     *     programmatically selecting text
     */
    function checkForSelectedText(e) {
      var e = e || window.event;
      var el = e.target || e.srcElement;
      var tagName = el.tagName && el.tagName.toLowerCase();
      var t;
      var d = document;
    
      // Try DOM 2 Range - for most browsers, including IE 6+
      // However, doesn't get text selected inside form controls
      // that allow selection of text content (input type text, 
      // textarea)
      if (d && d.selection && d.selection.createRange) {
        t = d.selection.createRange().text;
    
      // Otherwise try HTML5 - note that getSelection returns
      // a string with extra properties. This may also get
      // text within input and textarea
      } else if (d.getSelection) {
        t = d.getSelection();
      }
    
      // If didn't get any text, see if event was inside
      // inupt@type=text or textarea and look for text
      if (t == '') {
        if (tagName == 'textarea' || 
           (tagName == 'input' && el.type == 'text')) {
    
         // Check selectionStart/End as otherwise if no text
         // selected, IE returns entire text of element
         if (typeof el.selectionStart == 'number' && 
             el.selectionStart != el.selectionEnd) {
            t = el.value.substring(el.selectionStart, el.selectionEnd)
         }
        }
      }
      return t;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a method that computes the line number of a given text
I am looking for a method of reading emails using Pop3 in C# 2.0.
I am looking for a method to place some text onto the clipboard with
For years I've been following a great pattern called Target-Action which goes like this:
I am looking for a way to test BeginInvoke on an Action method, since
I am looking for a way to add an HTML element using JavaScript. But
I'm looking for method to add gradually fading or maybe blured border (I don't
I am looking for a method of storing Application Messages, such as You have
I'm looking for a method to assign variables with patterns in regular expressions with
I am looking for a method to compare and sort UTF-8 strings in C++

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.