1

I am trying to get the cursor position when I click a contenteditable element, I've already found the same question answered here and it works most of the time, but there is an issue in the following case :

http://jsfiddle.net/mody5/ts5pjoqd/4/

I added a margin of 5px for the contentEditable.

If you put the cursor at the beginning of the first line, it show the left position of 5px which is correct, if you put the cursor at the beginning of the second line it shows a big number than 5px

Note : the margin of 5px is added only for clarity reason, even without a margin the issue is still there

Is there a fix for that ?

the code of jsfiddle :

HTML :

<div contenteditable="true" style="margin:5px;">
<span>Lorem <em>ipsum</em> dolor</span> sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<code>Lorem ipsum</code> dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<br>
<strong style="font-weight:bold;">Coords :</strong> <span id="coords"></span>
</div>

javascript :

function getSelectionCoords(win) {
    win = win || window;
    var doc = win.document;
    var sel = doc.selection, range, rects, rect;
    var x = 0, y = 0;
    if (sel) {
        if (sel.type != "Control") {
            range = sel.createRange();
            range.collapse(true);
            x = range.boundingLeft;
            y = range.boundingTop;
        }
    } else if (win.getSelection) {
        sel = win.getSelection();
        if (sel.rangeCount) {
            range = sel.getRangeAt(0).cloneRange();
            if (range.getClientRects) {
                range.collapse(true);
                rects = range.getClientRects();
                if (rects.length > 0) {
                    rect = rects[0];
                }
                x = rect.left;
                y = rect.top;
            }
            // Fall back to inserting a temporary element
            if (x == 0 && y == 0) {
                var span = doc.createElement("span");
                if (span.getClientRects) {
                    // Ensure span has dimensions and position by
                    // adding a zero-width space character
                    span.appendChild( doc.createTextNode("\u200b") );
                    range.insertNode(span);
                    rect = span.getClientRects()[0];
                    x = rect.left;
                    y = rect.top;
                    var spanParent = span.parentNode;
                    spanParent.removeChild(span);

                    // Glue any broken text nodes back together
                    spanParent.normalize();
                }
            }
        }
    }
    return { x: x, y: y };
}

document.onmouseup = function() {
    var coords = getSelectionCoords(window);
    document.getElementById("coords").innerHTML = coords.x + ", " + coords.y;
};
Community
  • 1
  • 1
medBouzid
  • 7,484
  • 10
  • 56
  • 86

0 Answers0