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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:12:16+00:00 2026-05-24T08:12:16+00:00

I am using the Range to manipulate selected text. I would like to calculate

  • 0

I am using the Range to manipulate selected text. I would like to calculate the height from where someone started selecting text to where they finished.

I have tried a span to the beginning and end of the selected range and I can accurately calculate the height form that, but it changes the DOM and prevents me form doing some other range manipulations like highlighting previously selected text.

I have also tried collecting the position of the mosedown and mosueup positions but I need an accurate height from the top of the text selected to the bottom of the text where the selection was released and thats not always the case.

So I was wondering if there was a way to calculate the height of a text selection without changing the DOM?

  • 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-24T08:12:17+00:00Added an answer on May 24, 2026 at 8:12 am

    It depends which browsers you need to deal with. Here’s a function that will work in IE >= 4 and browsers that support getClientRects() in Range (Firefox >= 4, WebKit since 2009, Opera). If you need support for earlier browsers, you’ll need to modify the DOM, which is actually fine so long as you restore the DOM to its previous state.

    jsFiddle: http://jsfiddle.net/W84yW/

    Code:

    function getSelectionHeight() {
        var selHeight = 0;
        if (document.selection && document.selection.type == "Text") {
            var textRange = document.selection.createRange();
            selHeight = textRange.boundingHeight;
        } else if (window.getSelection) {
            var sel = window.getSelection();
            if (sel.rangeCount > 0) {
                var range = sel.getRangeAt(0);
                if (!range.collapsed && range.getClientRects) {
                    var startRange = range.cloneRange();
                    startRange.collapse(true);
                    var selTop = startRange.getClientRects()[0].top;
                    startRange.detach();
                    var endRange = range.cloneRange();
                    endRange.collapse(false);
                    selHeight = endRange.getClientRects()[0].bottom - selTop;
                    endRange.detach();
                }
            }
        }
        return selHeight;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to delete a range of data from a text file using PHP.
I'm copying a piece of text from Excel to Word using : Range(A1:C37).Copy appWD.Selection.PasteExcelTable
I would like to specify that firefox select a range. I can do this
I am reading a cell value from excel using named range in my case
I'd like to sort a range using a permutation stored in an array. I
How can I transform a selected text range into a HTML element? For example,
When I create a graph after using range.copy and range.paste it leaves the paste
I usually either end up using [range]%s/word/another_word/gc or do it manually. I wonder if
If I put a DateTime value into an Excel cell using Range.set_value through .Net
I'm trying to get the week range using Sunday as the start date, and

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.