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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:40:09+00:00 2026-06-15T16:40:09+00:00

Suppose my HTML looks something like this <div> <p> start<span>span</span>end </p> </div> I need

  • 0

Suppose my HTML looks something like this

<div>
    <p> start<span>span</span>end </p>
</div>

I need to be be able to get the offsets when a selection is made, without ignoring the span tags.

e.g.
suppose to user selected

t<span>span</span>e

I want the to get 4 as starting offset and 24 as the ending offset.
The selection object obtained via window.getSelection() returns 1,8, which is rather useless to me.
I obviously need to handle all cases where the user selects only the inner span, or part of the span etc.

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-06-15T16:40:10+00:00Added an answer on June 15, 2026 at 4:40 pm

    I’ve ended up solving it thusly:

                var range = sel.getRangeAt(0);
                // inserts two spans at the beginning and end of the range, to
                // calculate the offsets
                var div = document.createElement("span");
                div.setAttribute("class", START_NODE);
                range.insertNode(div);
                range.collapse(false);
                div = document.createElement("span");
                div.setAttribute("class", END_NODE);
                range.insertNode(div);
                // gets the offsets by looking up the location of the inserted spans
                // removes them after finding the offsets (also so the starting
                // offset won't screw up the ending offset)
                comment.startOffset = p.html().indexOf('<span class="' + START_NODE + '">');
                $("." + START_NODE).remove();
                comment.endOffset = p.html().indexOf('<span class="' + END_NODE + '">');
                $("." + END_NODE).remove();
                p.html(p.html());
    

    basically, I’ve added a SPAN at the beginning of the range selection, then collapsed the range and added a SPAN at the end of it.
    Then I simply searched for the index of the first span I’ve added, and the index of the second span I’ve added to find the offsets.

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

Sidebar

Related Questions

Suppose I have an HTML page that looks something like this: <html><body> 00123 <input
Suppose I have a view model with a property that looks something like this:
Suppose I have something like this: <form method=get action=token.php> <b>Enter token:</b><br> <input type=text name=token><BR>
Suppose a HTML markup like this: <ul> <li>List item 1</li> <li>List item 2</li> <li>List
Suppose I have the following HTML: <div id=test> <span style=display:inline;>foo</span> <span style=display:none;>bar</span> <span style=display:inline;>baz</span>
Suppose I have this html markup: <div id=wrapper> <pre class=highlight> $(function(){ // hide all
Suppose we have this html content, and we are willing to get Content1, Content2,..
in devise, the forms look like this: form_for(:resource, @resource, :url => edit_user(resource), :html =>
This is kind of surprising. Even though it looks to me like my submit
Suppose a HTML document has a iframe. Using javascript,I want to detect ,on load

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.