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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:18:30+00:00 2026-05-23T03:18:30+00:00

I augment text while it is written, like e.g. syntax highlighting via spans around

  • 0

I augment text while it is written, like e.g. syntax highlighting via spans around kewords, in a contentediatable field.

I have the common problem of the cursor jumping towards the begin of the contenteditable. I tried the solution by Nico Burns on Set cursor position on contentEditable <div> it worked in the example but not for my code, possibly because I change the DOM inside the contenteditable itself.

full code at: http://pastie.org/2060277

output.addEventListener('keyup',augmentInput,false);
        output.addEventListener('keydown',saveCursorPos,false);
        output.addEventListener('mousedown',saveCursorPos,false);
        output.addEventListener('keyup',restoreCursorPos,false);

        function saveCursorPos(e){
            //var selection = window.getSelection();
            savedRange = window.getSelection().getRangeAt(0);
            console.log("save "+e.type,savedRange);
        }

        function restoreCursorPos(e){
        document.getElementById("output").focus();

            if (savedRange != null) {
                var s = window.getSelection();

                if (s.rangeCount > 0){
                    s.removeAllRanges();
                }

                s.addRange(savedRange);
                console.log("range !=0 "+e.type, savedRange);
            }

            else {
                window.getSelection().addRange(savedRange);
                console.log("range ==0 "+e.type, savedRange);
            }
        }

striking:

  • That the Range is saved like it should on mouse click (in reference to the innermost element the cursor is in, with correct offset) but not if it is saved on keydown (than in reference to the contenteditable itself, startOffset always 0, not matter where the cursor was)
  • If I prevent the cursor to be saved on keydown and just use the mouseup to save the range it is resored like if it was never saved properly or it changed: reference to the contenteditable itself, startOffset always 0. So I wonder if the range object is changing e.g. reflecting changes in the DOM?
  • 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-23T03:18:31+00:00Added an answer on May 23, 2026 at 3:18 am

    Ranges do react to changes in the DOM and there are rules governing this. Whether changes affecting selected Ranges are reflected in the selection is not specified, and browser behaviour varies.

    If you’re making big changes to the DOM, I’d suggest using a different approach, such as the one used by the selection save/restore module in my Rangy library, which is to insert temporary invisible marker elements in the DOM at the start and end of the selection.

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

Sidebar

Related Questions

I have an autocomplete field which I would like to augment by providing a
I'd like to augment the Function object to do more operations while constructing a
I know that ALT text is supposed to augment the image ... be of
I would like to augment Eclipse CDT with my own plugin, without touching to
I have a few Python packages that I would like to tidy up and
I'd like to hide the Actions and Settings menus for some lists I have
I'm having problems trying to connect my website to Facebook. What I would like
I like the Stack Overflow comment UI a great deal and I'm looking into
I've been helping augment a twenty-some year old proprietary language within my company. It
Possible Duplicate: Most common or vicious mistakes in C# development for experienced C++ programmers

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.