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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:40:02+00:00 2026-06-13T12:40:02+00:00

I have a contenteditable div with an id of editable in which I’ve simulated

  • 0

I have a contenteditable div with an id of “editable” in which I’ve simulated changing the character associated with a key event. You can see it in action here: http://jsfiddle.net/Ukkmu/66/

As you can see, no matter what alphanumeric key the user presses, the output string of “Singinme.” types out in the div. The spaces between the words in my var list are ignored. I’d like the output to include spaces, and read “Sing in me.” as it is currently written in the first line of my javascript code. I’ve gotten help to at least identify that my problem is in the insertTextAtCursor function, but how do I change that function to include space as an acceptable output character? Or, could I re-write my var list in a way that would type out as “Sing in me.” in the div?

For convenience, I’ve pasted the javascript below as well.

PS: I just found out that this is a browser issue. In Chrome, it is as I described it. In IE it works as I want it- “Sing in me.” with spaces. Any ideas how to make this work in Chrome? I don’t know if it works in firefox.

var list = "Sing in me.".split('');
function transformTypedCharacter(charStr) {
    return (/[a-zA-Z]/).test(charStr) ? list.shift()||" " : charStr;
}

function insertTextAtCursor(text) {
    var sel, range, textNode;
    if (window.getSelection) {
        sel = window.getSelection();
        if (sel.getRangeAt && sel.rangeCount) {
            range = sel.getRangeAt(0).cloneRange();
            range.deleteContents();
            textNode = document.createTextNode(text);
            range.insertNode(textNode);

            // Move caret to the end of the newly inserted text node
            range.setStart(textNode, textNode.length);
            range.setEnd(textNode, textNode.length);
            sel.removeAllRanges();
            sel.addRange(range);
        }
    } else if (document.selection && document.selection.createRange) {
        range = document.selection.createRange();
        range.pasteHTML(text);
    }
}


$("#editable").keypress(function(evt) {
    if (evt.which) {
        var charStr = String.fromCharCode(evt.which);
        var transformedChar = transformTypedCharacter(charStr);
        if (transformedChar != charStr) {
            insertTextAtCursor(transformedChar);
            return false;
        }
    }
});

​

  • 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-13T12:40:03+00:00Added an answer on June 13, 2026 at 12:40 pm

    Chrome swallows the whitespace nodes. I can’t tell whether this is a bug or not since as it seems multiple browsers treat whitespace and empty nodes differently.

    In your case a simple solution is using non-breaking spaces instead. That’s:

    var list = "Sign\xA0in\xA0me.".split("");
    

    Or

    var list = "Sign in me.".replace(/\s/g, "\xA0").split("")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a div which is set to be editable <div class=.textarea_mask contenteditable ></div>
I have a editable div: <div id=artikel contenteditable=true></div> It works nice, I can edit
I have a content editable div where users can enter their bio and then
Let's say I have a contentEditable div , to the user can edit and
For example, I have a contentEditable div and I can type in it. When
I have a standard content editable div: <div id="profileBody" class="customInputDiv" contentEditable='true' autocomplete="off"></div> Basically I
I have a contenteditable div on my site which works great. On Chrome i
have this html: <div id=editable contentEditable=true > <span contentEditable=false >Text to delete</span> </div> need
I have a contentEditable div within which I need to allow the user to
I have a contentEditable div in a UIWebView. I want to disable the spell

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.