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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:19:47+00:00 2026-06-01T08:19:47+00:00

I am making use of a contenteditable div in combination with the rangy Javascript

  • 0

I am making use of a contenteditable div in combination with the rangy Javascript library to insert HTML at the cursor position.

End of the day the contents of the div commonly looks like:

<div contenteditable="true">
    "Hello "
    <button contenteditable="false" data-id="147">@John Smith</button>
    " "
</div>

Users get suggested upon pressing ‘@’ and get subsequently inserted as a button when selected (ala Google Plus). I also insert a &nbsp; after this button.

The button gets removed in Chrome/Safari/Firefox when you hit backspace (after first removing the &nbsp;), but not in IE8. In IE8 the cursor merely jumps over the button without removing it. What’s even more bizarre in IE8 is if you leave the &nbsp; next to the button – and rather place the cursor right next to the button – it removes the button on backspace. So it’s happy when there’s a &nbsp; to the right of the cursor.

Does anyone know what I need in order to make IE8 work i.t.o. removing the button upon backspace without the need for a &nbsp; to the right of the cursor? (some info on this strange behaviour might also help)

P.S. I haven’t tested other versions of IE

  • 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-01T08:19:49+00:00Added an answer on June 1, 2026 at 8:19 am

    My suggestion would be to first check the caret is positioned after the non-editable node, and if so, create a range that starts immediately after the non-editable element and ends at the caret position. Check whether the text in this range is empty. If it is, that means the caret is placed directly after the non-editable element, so in that case remove the element. Finally, place the caret where the non-editable had been.

    Live demo: http://jsfiddle.net/timdown/vu3ub/

    Code:

    document.onkeypress = function(e) {
        e = e || window.event;
        var keyCode = e.which || e.keyCode;
        if (keyCode !== 8) {
            return;
        }
    
        var sel = rangy.getSelection();
        if (sel.rangeCount === 0) {
            return;
        }
    
        var selRange = sel.getRangeAt(0);
        if (!selRange.collapsed) {
            return;
        }
    
        var nonEditable = document.getElementById("nonEditable");
        if (!nonEditable) {
            return;
        }
    
        // Check the caret is located after the non-editable element
        var range = rangy.createRange();
        range.collapseAfter(nonEditable);
    
        if (selRange.compareBoundaryPoints(range.START_TO_END, range) == -1) {
            return;
        }
    
        // Check whether there is any text between the caret and the
        // non-editable element. If not, delete the element and move
        // the caret to where it had been
        range.setEnd(selRange.startContainer, selRange.startOffset);
        if (range.toString() === "") {
            selRange.collapseBefore(nonEditable);
            nonEditable.parentNode.removeChild(nonEditable);
            sel.setSingleRange(selRange);
    
            // Prevent the default browser behaviour
            return false;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making use of a custom cursor on itemRenderers in a List component.
I use the MooTools Javascript library. And this library give a couple of really
I'm making use of Eigen library which promises vectorization of matrix operations. I don't
I am making use of HTML tidy and I've had a look at the
I am making use of BULK INSERT within a stored procedure and I get
I am making use of this excellent library called Tank Auth . Hopefully this
I am making use of javamail and I am having trouble getting the HTML
I stumbled upon some code the other day that was making use of query
I'm making use of a List<? extends Map<String,?>> that I populated with data. cursor.moveToFirst();
I am making use of a tablesorter library . My issue is that every

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.