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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:11:35+00:00 2026-05-26T18:11:35+00:00

I have an HTML element with only visible text inside. This example is a

  • 0

I have an HTML element with only visible text inside. This example is a <div> element, but it could be a <span>, <p>, or other DOM element.

<div>This is a simple example.</div>

When clicked, I can get the position of the cursor on the surface of the div, but I need to determine the position of the nearest character and/or its index into the div.innerHTML string at the time of the click.

I found a similar implementation in the “getCharNumAtPosition” method in SVG text entities here.

Is it possible to implement such a function in JavaScript that works with HTML?

(Solutions would be most useful if they are portable across most modern browsers, work with most written languages, and are based on relatively stable standards so that they will not become buggy later.)

  • 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-26T18:11:36+00:00Added an answer on May 26, 2026 at 6:11 pm
    $('div').click( function () {
      getSelectionPosition (); 
    });
    
    
    function getSelectionPosition () {
      var selection = window.getSelection();
      console.log(selection.focusNode.data[selection.focusOffset]);
      alert(selection.focusOffset);
    }
    

    This works with “click”, as well as with a “range” for most browsers. (selection.type = "caret" / selection.type = "range").

    selection.focusOffset() gives you the position in the inner node. If elements are nested, within <b> or <span> tags for example, it will give you the position inside the inner element, not the full text, more or less. I’m unable to “select” the first letter of a sub tag with focusOffset and “caret” type (click, not range select). When you click on the first letter, it gives the position of the last element before the start of tag plus 1. When you click on the second letter, it correctly gives you “1”. But I didn’t find a way to access the first element (offset 0) of the sub element. This “selection/range” stuff seems buggy (or very non-intuitive to me). ^^

    But it’s quite simple to use without nested elements! (Works fine with your <div>)

    Here is a fiddle

    Important edit 2015-01-18:

    This answer worked back when it was accepted, but not anymore, for reasons given below. Other answers are now most useful.

    • Matthew’s general answer
    • The working example provided by Douglas Daseeco.

    Both Firefox and Chrome debugged window.getSelection() behavior. Sadly, it is now useless for this use case. (Reading documentation, IE 9 and beyond shall behave the same).

    Now, the middle of a character is used to decide the offset. That means that clicking on a character can give back 2 results. 0 or 1 for the first character, 1 or 2 for second, etc.

    I updated the JSFiddle example.

    Please note that if you resize the window (Ctrl + mouse), the behavior is quite buggy on Chrome for some clicks.

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

Sidebar

Related Questions

is it possible to have a visible html element but one which lacks presence
I have an HTML element with a title inside, like this. <details>Name of page</details>
How can I remove certain element inside html()? For instance, I have this html
When you set an html element to have display: none , the content inside
I have an HTML string representing an element: '<li>text</li>' . I'd like to append
I have overlay inside HTML element and inside that overlay I have element that
This should have been pretty straightforward using the DOM but I seem to be
Refer to this link (open firebug) . I have a dropdown html element which
I have HTML page with some HTML element with ID=logo . I need to
I have an HTML element with a large collection of unordered lists contained within

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.