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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:55:27+00:00 2026-05-25T10:55:27+00:00

How can I transform a selected text range into a HTML element? For example,

  • 0

How can I transform a selected text range into a HTML element?

For example, string The quick brown fox jumps over the lazy dog, I’ve fetched the part brown that reflects selection points start = 10 and end = 14 (selection isn’t built by user input).

Now, how do I transform this part of string into a <span>brown</span>?

P.S. I’ve looked into How to wrap with HTML tags a cross-boundary DOM selection range?. The answer provided uses execCommand, but that doesn’t satisfy me, because I need this to be as transparent as possible.

I’m using Range API for selections too, but as in this case- the “selection” is just stored pointers of start/end locations with no selection actually made.

I had an idea that I could use the pointers to create a selection in the background, wrap with </span> and that would be invisible to user, but then again… I have no idea how to execute this.

Thanks in advance!

  • 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-25T10:55:28+00:00Added an answer on May 25, 2026 at 10:55 am

    You can do this in this case using methods of Range. I’m assuming your text is all contained in a single text node. For example:

    <div id="test">The quick brown fox jumps over the lazy dog</div>
    

    First, create the range:

    var range = document.createRange();
    var textNode = document.getElementById("test").firstChild;
    range.setStart(textNode, 10);
    range.setEnd(textNode, 15);
    

    To surround just the word “brown” within a span, you can use the range’s surroundContents() method:

    var span = document.createElement("span");
    range.surroundContents(span);
    

    However, this won’t work in some more complicated cases where the selection crosses node boundaries. Also, IE < 9 does not support Range, so you’d need a completely different solution for those browsers.

    Live demo: http://jsfiddle.net/m3yJ5/

    Self-promotion section

    For more complicated cases, you could use my Rangy library and its CSS class applier module, which surrounds any arbitrary selection or range in <span>s with a particular CSS class.

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

Sidebar

Related Questions

How can I transform a String value into an InputStreamReader ?
How can I transform a website to be able to handle multi language (example
How can I transform a time value into YYYY-MM-DD format in Java? long lastmodified
In SQL server How can I transform 1 row with varchar columns into a
Can I get Visual Studio to transform the built-in aliases into the System types?
One can get the text of the selected item in the list-view of a
I'm exporting some mysql data to a text file. The data can be selected
I have a scene and an object placed in some coordinates. I can transform
Can somebody give a good explanation of FFT image transform How the FFT transformed
Can any of STL algorithms/container operations like std::fill , std::transform be executed in parallel

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.