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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:09:17+00:00 2026-06-05T23:09:17+00:00

I don’t know if the title is right. The thing is that I have

  • 0

I don’t know if the title is right. The thing is that I have found a snippet to get the cursor position when it’s focused in a contenteditable element. I understand the whole code, but there are five lines that I do no t know for what they are good.

Ok. This lines are a function, which is passed to a Treewalker as a parameter. And they deal with comparing boundary points, but, how, and for what?. I have been researching lot of days, and haven’t found ists functionality yet.

function(node) {
        var nodeRange = document.createRange();
        nodeRange.selectNode(node);
        return nodeRange.compareBoundaryPoints(Range.END_TO_END, range) < 1 ?
            NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
    }

The whole snippet is this (just to situate it in context):

function getCharacterOffsetWithin(range, node) {
var treeWalker = document.createTreeWalker(
    node,
    NodeFilter.SHOW_TEXT,
    function(node) {
        var nodeRange = document.createRange();
        nodeRange.selectNode(node);
        return nodeRange.compareBoundaryPoints(Range.END_TO_END, range) < 1 ?
            NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
    },
    false
);

var charCount = 0;
while (treeWalker.nextNode()) {
    charCount += treeWalker.currentNode.length;
}
if (range.startContainer.nodeType == 3) {
    charCount += range.startOffset;
}
return charCount;
}

document.body.addEventListener("keyup", function() {
  var el = document.getElementById("test");
  var range = window.getSelection().getRangeAt(0);
  document.getElementById("position").innerHTML = "Caret char pos: " + getCharacterOffsetWithin(range, el);
}, false);

It can be seen on work here: JS Fiddle

Thank you a lot, I just want to understand this stuff before working with it. Sorry for the long Post.

  • 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-05T23:09:19+00:00Added an answer on June 5, 2026 at 11:09 pm

    This looks like my code, from this question. It’s overly complicated for the task it’s trying to accomplish and I provided a simpler alternative to a similar question that I would recommend using instead. I’ve now updated the original answer with a link to the simpler answer.

    As to your question about what that function does, it’s there to filter nodes. The TreeWalker visits all text nodes within the container node and yields only nodes those that are located before the end of the selection. The filter function is called on each text node, creates a range encompassing the text node and checks whether the end of the text node is earlier in the document than the end of the selection range using compareBoundaryPoints().

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

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
don't know if the title describes anything about what I'm trying to say but
Don't really know how to formulate the title, but it should be pretty obvious
Don't know if I worded the question right, but basically what I want to
Don't know how to explain it better but i'm trying to get a response
Don't these two mean the same thing, first get the value and then increment?
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't know if this is the right place to ask this, but I will
Don't know if this is possible, but I have some code like this: val
Don't know if this has been answered before. Have custom routes to users. If

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.