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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:55:43+00:00 2026-05-26T20:55:43+00:00

So i have a pre tag like so: <pre> Some content, more content. <span>Coloured

  • 0

So i have a pre tag like so:

<pre> Some content, more content. <span>Coloured content</span>. Some more content</pre>

What i want to do is setup an event using javascript or jquery that binds a mouseup event. When the user selects text, i want to get the indexes offset from the start of the pre, so it ignores the span tags per say. So if someone selects the text after the span tag, it knows to offset from the pre opening.

Is there a way I can do this? It looks like window.getSelection starts it off after the span tag.

  • 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-26T20:55:43+00:00Added an answer on May 26, 2026 at 8:55 pm

    Given this HTML

    <pre>0<span>1</span>23<span>4<span>56<span><span>7</span></span>8</span></span></pre>
    

    you want to get the first selected digit as output/offset, right?
    The basic idea is to navigate to the left in the DOM tree until there is no more node with the same parent. Then climb up to finally reach the pre tag. Whilst navigating through the tree towards the upper left, all characters of the visited elements are counted and added to the final result.

    $('pre').on('mouseup', function(){
        var selection = window.getSelection();
        // Get the offset within the container that holds all of the selection
        var offset = selection.anchorOffset;
        // A reference to the currently examined node
        var currentNode = selection.anchorNode;
        // Wander around until we hit the pre
        while(currentNode!==this){
            if(currentNode.previousSibling){
                // If there is a node left of us (with the same parent) navigate to the left and sum up the text length in the left node.
                // There is no need to check the children (possibly existent) since these would be included in text's return value
                offset = offset + $(currentNode.previousSibling).text().length;
                // Navigate to the left node of the current
                currentNode = currentNode.previousSibling;
            } else {
                // There is no left node so climb up towards the pre tag
                currentNode = currentNode.parentNode;
            }
        }
        // Print the final result
        console.log(offset);
    });
    

    The script should output the required number. So if you are selecting 78 you’d get 7 as output.

    I did only test this code in Firefox. Other browsers should work as well if they implement HTML Editing API. IE does not support it until version 9. The same applies for getSelection (see MSDN).

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

Sidebar

Related Questions

I have a <pre> & <code> tag which looks like: <pre><code>somevar = array.join("-") somevar.split("-")
I have all my content pre-escaped, so rather than using the triple stash everywhere
If I have html like this: <li id=listItem> This is some text <span id=firstSpan>First
I have a content editable div. Within this I am inserting a <pre>some code</pre>
I have a problem with outputting some C++ inside a pre tag. The XHTML
So I have a div with some pre tags in it, like so: <div
I have a pre tag within my page that contains quite a bit of
I have a project that comprises pre-build Dll modules, built some time in the
I have a form that I pre-populate with test data though jQuery. $('INPUT[name=subscription.FirstName]').val('Jef'); but
I need to pre-compress some very large html/xml/json files (large data dumps) using either

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.