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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:20:46+00:00 2026-05-23T04:20:46+00:00

I ‘de like to higlight text selected by the user and copy it over

  • 0

I ‘de like to higlight text selected by the user and copy it over to a text box.

The latter part works great. But highlighting of the selected text fails if I try to highlight part of an already highlighted text.

Here’s a JSFiddle
Try selecting the text on the right box, then try to select part of your previous selection.

Any ideas?

The relevant code:

function highlightSelected() {
var selection = getSelected();
var range = selection.getRangeAt(0);
var newNode = document.createElement("span");
$(newNode).addClass('mark');
range.surroundContents(newNode);
return range;
}

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-23T04:20:47+00:00Added an answer on May 23, 2026 at 4:20 am

    surroundContents() can only work if surrounding the entire range within a single element produces valid HTML (that definition is rather loose: see the spec for a proper definition). For example, the following range (denoted by pipes) can be surrounded using surroundContents():

    <p>fo|o<i>bar</i>ba|z</p>
    

    … while the following cannot:

    <p>fo|o<i>ba|r</i>baz</p>
    

    What your options are depends on exactly what you’re trying to achieve. If you’re just trying to change the background colour of the selection, you can use document.execCommand() for that. If you must add a class to the whole selection contents, you need something a little more complex, such as the class applier module of my Rangy library.

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

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I have just tried to save a simple *.rtf file with some websites and
I was writing code for dragging mechanism which invokes to wait for small period
I downloaded PostgreSQL from the official website and ran the .dmg installer. After that
I am trying to create a RegEx expression that will successfully parse the following

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.