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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:25:04+00:00 2026-05-21T14:25:04+00:00

after skimming all possible questions and answers, i’ll try it this way. I’m programming

  • 0

after skimming all possible questions and answers, i’ll try it this way.

I’m programming an RTE, but didn’t manage to successfully extract text in a contenteditable element.
The reason for this is, that each browser handles nodes and keypress (#13) events in a slightly different way (as ex.g. one creates ‘br’, the other ‘div’, ‘p’, etc.)
To keep this all consistent, I’m writing a cross-browser editor which kills all default action with e.preventDefault();

Following scenario:

1) User hits the #13 key (check)

2) Caret position detected (check)

3) create new p(aragraph) after the element the caret’s in (check)

4) if text(node(s)) between caret and the element’s end, extract it (? ? ?)

5) put text(node(s)) to newly created p(aragraph) (check)

As you can imagine, everything works except point 4.

There’s a similar functionality in the well-known js rangy library, where a specific selection is being extracted.

What i need is following: Get and extract all text (with tags of course, so splitBoundaries) from the caret on to the end of the contenteditable paragraph (p, h1, h2, …) element.

Any clues, hints or snippets are welcome!
Thanks in advance.

Kind regards,
p

  • 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-21T14:25:05+00:00Added an answer on May 21, 2026 at 2:25 pm

    You can do this by creating a Range object that encompasses the content from the caret to the end of the containing block element and calling its extractContents() method:

    function getBlockContainer(node) {
        while (node) {
            // Example block elements below, you may want to add more
            if (node.nodeType == 1 && /^(P|H[1-6]|DIV)$/i.test(node.nodeName)) {
                return node;
            }
            node = node.parentNode;
        }
    }
    
    function extractBlockContentsFromCaret() {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var selRange = sel.getRangeAt(0);
            var blockEl = getBlockContainer(selRange.endContainer);
            if (blockEl) {
                var range = selRange.cloneRange();
                range.selectNodeContents(blockEl);
                range.setStart(selRange.endContainer, selRange.endOffset);
                return range.extractContents();
            }
        }
    }
    

    This won’t work in IE <= 8, which doesn’t support Range or the same selection object as other browsers. You can use Rangy (which you mentioned) to provide IE support. Just replace window.getSelection() with rangy.getSelection().

    jsFiddle: http://jsfiddle.net/LwXvk/3/

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

Sidebar

Related Questions

After executing this query on master db ,it is giving me all running process
After watching the webinar, skimming over the BuckyBook PDF, and following the Eclipse RCP
After deploying WCF server (svc) on my Server, I have got this message when
After discovering about Javascript namespaces, I tried to implement them but I run into
After I click update in the grid view, the code works successfully. But when
Before you jump to conclusions, yes, this is programming related. It covers a situation
After doing an AJAX call, am redirecting- using the following code. But, after AJAX
After calling exec, is it possible to print a message, because I tried and
After looking at this question , I think I want to wrap ThreadLocal to
After reading many times this question and its accepted answer How to execute a

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.