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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:33:33+00:00 2026-06-17T09:33:33+00:00

I have an eventHandler that is trigered every time a node is inserted in

  • 0

I have an eventHandler that is trigered every time a node is inserted in a contenteditable div. This eventHandler replaces a newly inserted div node with a p node.

The problem is placing the cursor after the replacement. Basically the cursor isn’t placed properly, that is, the cursor disappears while it was expected to be placed in the new created p node. Strangely the content editable div still has focus and dumping the window.selection object shows the range correctly set.

The only way I could workaround this issue was with a dirty fix using setTimeout.


Q. Why does it work when placeCursor() is called with setTimeout but not without it?


Relevant Code:

JSFiddle

obj.addEventListener("DOMNodeInserted", onNodeInsert, false);

function onNodeInsert(e) {
    var range    = document.createRange(),
        sel      = window.getSelection(),
        newNode  = e.target,
        tagName  = newNode.tagName.toLowerCase(),
        lnbrNode = document.createElement('br'),
        pNode    = document.createElement('p');

    if (tagName === 'div' && newNode.getAttribute("id") === null) {
        // First we remove the event listener so that it doesn't get triggered again
        this.removeEventListener('DOMNodeInserted', onNodeInsert, false);

        // Creates a p node and removes the div
        newNode.parentNode.replaceChild(pNode, newNode);
        pNode.appendChild(lnbrNode);

        // Places the caret where it belongs
        var placeCursor = function () {
            range.setStart(pNode, 0);
            sel.removeAllRanges();
            sel.addRange(range);    
        }

        //placeCursor(); // DOES NOT WORK (cursor disappears)
        setTimeout(placeCursor,1); // WORKS

        //We can restore the event listener now
        this.addEventListener("DOMNodeInserted", onNodeInsert, false);
    }
}

for more context see this post

  • 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-17T09:33:34+00:00Added an answer on June 17, 2026 at 9:33 am

    I don’t know how to do a good explain, but is because the DOM persistence delay. I have lot of problems with mobile. So the dom isn’t ready when the listener callback is executed.

    So it wont work because you are trying to manage a selection. If you put a timeout, that function will be execute after 4ms, so it’s enough time to DOM persist your changes with range and make the changes you are trying to do.

    Indeed the browser should not need defer the executing of function to work, in theory your code is correct without the setTimeout, but this “bug” you can find in many ways when working with listeners that are triggered by DOM changing.

    Just to you know. In my desktop browser, it work like it should, but, when I ran it in my netbook, I need to use setTimout to work.

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

Sidebar

Related Questions

I have an eventhandler function for a textbox, that does the following, Searches the
Hi I use GWT and I have a com.smartgwt.client.widgets.Button that has the following eventHandler:
I have a jQuery event handler that reacts to every change in an: <input
I have recognized, that eventhandler added with addEventListener where not influenced by $.trigger .
This is not so much a problem, just something that I would like to
my web application is based on dojo 1.6.0. The problem that I have is
I have an event handler that needs to determine a type and execute code
I have an event handler that will remove an element from a list of
I want to have an event handler that executes only once and then unbinds
In my Silverlight app I have an event handler that dynamically creates a new

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.