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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:15:47+00:00 2026-05-25T20:15:47+00:00

I have a contenteditable div which contains typical wysiwyg editor html (bold, anchors, lists).

  • 0

I have a contenteditable div which contains typical wysiwyg editor html (bold, anchors, lists).

I need to determine if the current cursor is, onKeyDown, at the start and at the end of the div. The reason for this is, based on the cursor position, and the key pressed, I might want to merge this div with the previous div on a backspace, or create a new following div on enter.

I’ve been fiddling around with ranges, but when you’re working with html inside the element, things get pretty complicated.

I’m hoping I must be overlooking some simple solution.

Is there a relatively simple way to determine this – I’m open to using a library like Rangy.

Thanks!

Edit: I’m thinking something along these lines:

$('.mycontenteditable').bind('keydown', handle_keydown)

handle_keydown = function(e) {

  range = window.getSelection().getRangeAt(0)
  start_range = document.createRange()
  start_range.selectNodeContents(this.firstChild)
  start_range.collapse(true) // collapse to start
  is_start = start_range.compareBoundaryPoints(Range.START_TO_START,range)
  end_range = document.createRange()
  end_range.selectNodeContents(this.lastChild)
  end_range.collapse(false)
  is_end = end_range.compareBoundaryPoints(Range.END_TO_END,range)
}

Am I going to run into any odd issues with something like this?

  • 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-25T20:15:48+00:00Added an answer on May 25, 2026 at 8:15 pm

    This is how I ended up solving this. My proposed solution above worked sometimes but there were way to many edge cases, so I ended up considering how much text was before or after the cursor, and if that was 0 characters, then I was at the start or end:

    handle_keydown = function(e) {
      // Get the current cusor position
      range = window.getSelection().getRangeAt(0)
      // Create a new range to deal with text before the cursor
      pre_range = document.createRange();
      // Have this range select the entire contents of the editable div
      pre_range.selectNodeContents(this);
      // Set the end point of this range to the start point of the cursor
      pre_range.setEnd(range.startContainer, range.startOffset);
      // Fetch the contents of this range (text before the cursor)
      this_text = pre_range.cloneContents();
      // If the text's length is 0, we're at the start of the div.
      at_start = this_text.textContent.length === 0;
      // Rinse and repeat for text after the cursor to determine if we're at the end.
      post_range = document.createRange();
      post_range.selectNodeContents(this);
      post_range.setStart(range.endContainer, range.endOffset);
      next_text = post_range.cloneContents();
      at_end = next_text.textContent.length === 0;
    }
    

    Still not entirely sure there are any other edge cases, as I’m not entirely sure how to unit test this, since it requires mouse interaction – there’s probably a library to deal with this somewhere.

    • 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
have this html: <div id=editable contentEditable=true > <span contentEditable=false >Text to delete</span> </div> need
I have a contenteditable div where I need to insert text at the caret
I have a div with contenteditable set. This div contains an inner div like
I have a contentEditable div where I want to insert HTML tags (a simple
I have a contenteditable div on my site which works great. On Chrome i
I'm trying to write a wysiwyg editor using a contentEditable div, and am having
I have a contentEditable DIV (building a kind of editor component). I am looking
I have a contentEditable div which in some cases gets display: none , then
I have a contentEditable div which in some cases replaces text with images .

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.