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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:16:19+00:00 2026-06-13T06:16:19+00:00

I have div with some text and contenteditable=true. When I single click on this

  • 0

I have div with some text and contenteditable=”true”. When I single click on this div – works some my scripts, it is not very important. And when I double click on this div – need to edit text in div. Edit text need to be only after double click, not after single. And very imortant, when I double click on div – caret need stay under mouse cursor. No need selection text. I found some script for single/double. But have problem. When I double click on div – text are selection. Selection no need. Need editor caret where I clicked. I do not understand how.
http://jsfiddle.net/X6auM/

  • 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-13T06:16:21+00:00Added an answer on June 13, 2026 at 6:16 am

    Every current major browser provides an API to create a range from a mouse event, although there are four different code branches needed.

    Here is some background:

    • https://stackoverflow.com/a/10659990/96100
    • https://stackoverflow.com/a/12705894/96100
    • Creating a collapsed range from a pixel position in FF/Webkit

    Here’s a demo: http://jsfiddle.net/timdown/krtTD/10/

    And here’s some code:

    function getMouseEventCaretRange(evt) {
        var range, x = evt.clientX, y = evt.clientY;
    
        // Try the simple IE way first
        if (document.body.createTextRange) {
            range = document.body.createTextRange();
            range.moveToPoint(x, y);
        }
    
        else if (typeof document.createRange != "undefined") {
            // Try Mozilla's rangeOffset and rangeParent properties,
            // which are exactly what we want
            if (typeof evt.rangeParent != "undefined") {
                range = document.createRange();
                range.setStart(evt.rangeParent, evt.rangeOffset);
                range.collapse(true);
            }
    
            // Try the standards-based way next
            else if (document.caretPositionFromPoint) {
                var pos = document.caretPositionFromPoint(x, y);
                range = document.createRange();
                range.setStart(pos.offsetNode, pos.offset);
                range.collapse(true);
            }
    
            // Next, the WebKit way
            else if (document.caretRangeFromPoint) {
                range = document.caretRangeFromPoint(x, y);
            }
        }
    
        return range;
    }
    
    function selectRange(range) {
        if (range) {
            if (typeof range.select != "undefined") {
                range.select();
            } else if (typeof window.getSelection != "undefined") {
                var sel = window.getSelection();
                sel.removeAllRanges();
                sel.addRange(range);
            }
        }
    }
    
    document.getElementById("editor").ondblclick = function(evt) {
        evt = evt || window.event;
        this.contentEditable = true;
        this.focus();
        var caretRange = getMouseEventCaretRange(evt);
    
        // Set a timer to allow the selection to happen and the dust settle first
        window.setTimeout(function() {
            selectRange(caretRange);
        }, 10);
        return false;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a div ( contenteditable=true ) which contains some text and some html.
I have a div which has some text, but according to different situations this
I have this div in my code: <div id=advice class=validation-advice style=>some text here</div> Now,
I have some text that is stored in a variable like this: <div class=foo>text
i have this schema: <div id=container> <div id=content>some text</div> <footer> my footer things </footer>
I have a div ( contenteditable=true ) which contains some html. I'm trying to
This is the structure of my HTML: <div> <span>Some text</span> <br/> <span>Not much text</span>
I have contenteditable div with text on it. In firefox, there is some kind
I have a div containing some text. This div used to be position:fixed ,
I have div with draggable , resizable and editable (contenteditable=true) functionality. This div contain

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.