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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:11:31+00:00 2026-06-11T19:11:31+00:00

I have a span inside the contenteditable div and I need to figure out

  • 0

I have a span inside the contenteditable div and I need to figure out if the cursor is in the last position on the span tag. I looked in the Selection and Range solutions but couldn’t figure out a straight forward way to achieve that.

<html>
    <script>
        var selection =  window.getSelection();
        var range = selection.getRangeAt(0);
        //this is what I am trying to achive
        var selection_target = range.selectNodeContents(document.getElementById('target')); 
        var length = selection_target.toString().length;

     </script>
     <body>  
          <div id='target' contenteditable='true'>
             <span>some text(figure cursor position here)</span>
          </div>
      </body>
</html>
  • 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-11T19:11:32+00:00Added an answer on June 11, 2026 at 7:11 pm

    For the case in the question where the span only has one child that is a text node, you can check whether the caret is at the end of it as follows:

    Demo: http://jsfiddle.net/Wm5Hz/

    Code:

    var span = document.getElementById("target").getElementsByTagName("span")[0];
    var spanTextNode = span.lastChild;
    var sel = window.getSelection();
    var isCaretAtEndOfSpan = (sel.isCollapsed
        && sel.focusNode == spanTextNode
        && sel.focusOffset == spanTextNode.data.length);
    

    Things are slightly more complicated in the general case: the same visual position can be represented in different ways (e.g. after the last character in the text node, after the text node itself, after the end of the span), plus if the span could contain elements then you’d need to find the last text node within the span and compare the selection against it. Also, none of this works in IE < 9, which has a completely different API.

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

Sidebar

Related Questions

I current have a need to have a contenteditable div tag inside an li
I have a contenteditable div . Inside, I have put a 'fraction' span, with
I have a CONTENTEDITABLE div and inside that div I have a CONTENTEDITABLE span,
have this html: <div id=editable contentEditable=true > <span contentEditable=false >Text to delete</span> </div> need
I have two span tags inside two divs inside a master div <div> <div>
I have a javascript code that have span tag and inside the span tag
I have a div that has a span inside that will display a message
I have <span> inside a <div> , like so: <div id=#mydiv> <a href=#>Hello</a><span class=myImageArea></span>
I have a span inside a legend tag that I want to use to
I have a span, which is inside a div of fixed width. The span

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.