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

The Archive Base Latest Questions

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

Can anyone tell me why this code is returning undefined . I was hoping

  • 0

Can anyone tell me why this code is returning undefined.

I was hoping this would give the left coordinate of the user selected text.

function alertRangeObject(){
    var userSelection;

    if(window.getSelection)
    { 
    userSelection = window.getSelection();
    }
    else if (document.selection)
    {
    userSelection = document.selection.createRange();
    }

    var selectedText = userSelection;
    if (userSelection.text)
    {
        selectedText = userSelection.text;
    }

    var rangeObject = getRangeObject(userSelection);

    function getRangeObject(selectionObject)
    {
        if (selectionObject.getRangeAt)
        return selectionObject.getRangeAt(0);
        else { //safari
            var range = document.createRange();
            range.setStart(selectionObject.anchorNode, selectionObject.anchorOffset);
            range.setEnd(SelectionObject.focusNode, selectionObject.focusOffset);
            return range;
              }
        }


       alert(rangeObject.offsetLeft);
   }

The frustrating thing is when you alert(rangeObject) I get the selected text. But I was I thought that if this was a text range I could use the offsetLeft method to get the left coordinates. Can anyone see what I’m doing wrong.
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-24T19:27:56+00:00Added an answer on May 24, 2026 at 7:27 pm

    TextRange objects do indeed have offsetLeft and offsetTop properties, but the code you’ve posted will throw an error in IE < 9 because the getRangeObject() function returns undefined. I’d suggest alternative code. The following will get you the top left coordinates of the selection relative to the viewport in IE >= 4 and recent versions of other browsers that support the getBoundingClientRect() method of Range, and returns (0, 0) in other browsers.

    jsFiddle: http://jsfiddle.net/NkSmn/

    Code:

    function getSelectionTopLeft() {
        var x = 0, y = 0;
        // Use standards-based method only if Range has getBoundingClientRect
        if (window.getSelection && document.createRange
        && typeof document.createRange().getBoundingClientRect != "undefined") {
            var sel = window.getSelection();
            if (sel.rangeCount > 0) {
                var rect = sel.getRangeAt(0).getBoundingClientRect();
                x = rect.left;
                y = rect.top;
            }
        } else if (document.selection && document.selection.type != "Control") {
            // All versions of IE
            var textRange = document.selection.createRange();
            x = textRange.boundingLeft;
            y = textRange.boundingTop;
        }
        return { x: x, y: y };
    }            
    

    I’m working on a Range/Selection position module for Rangy at the moment that uses a similar approach to the above. It’s a work in progress but works pretty well in most scenarios and browsers. It should make it into version 1.3. Demo here: http://rangy.googlecode.com/svn/trunk/demos/position.html

    As an aside, that code looks like it’s based on code from PPK’s introduction to Ranges page on quirksmode. It’s out of date and the code is confusing and not ideal, but there should be some improvement there in the near future.

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

Sidebar

Related Questions

can anyone tell me why this code don't work: $q = $_GET['q']; // Load
Can anyone tell me what's wrong with this code? class Dataset < ActiveRecord::Base has_many
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
Can anyone tell me what exactly does this Java code do? SecureRandom random =
I stumbled across this C code today. Can anyone tell me what the 'where'
Can anyone tell me what the correct Plinq code is for this? I'm adding
Can anyone tell me why this would generate a syntax error in Safari and
Can anyone please tell why this code doesn't work: $ser = array('one','two','three','four'); $x =
Can anyone tell me what's wrong in this code: #!/usr/local/bin/python import os import string,
Can anyone tell me why is NSAutoreleasePool leaking in this code. I am using

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.