0

I'd like to know how to move the cursor position to the end of my text wrapped by a "contenteditable" DIV...

My code below selects all my text in the DIV element... But I just would like to add the cursor at the end of the text without selecting all the text...

My .js code:

  $mydiv.wrapInner('<div contenteditable="true" id="editableCase"></div>');
  var range = document.createRange();
  range.selectNodeContents(document.getElementById("editableCase"));
  var sel = window.getSelection();
  sel.removeAllRanges();
  sel.addRange(range);
  _case.$element.children().children().focus();
user3241019
  • 811
  • 9
  • 20
tonymx227
  • 5,293
  • 16
  • 48
  • 91

1 Answers1

1

This is the common answer to this issue :

function placeCaretAtEnd(el) {
        el.focus();
        if (typeof window.getSelection != "undefined"
                && typeof document.createRange != "undefined") {
            var range = document.createRange();
            range.selectNodeContents(el);
            range.collapse(false);
            var sel = window.getSelection();
            sel.removeAllRanges();
            sel.addRange(range);
        } else if (typeof document.body.createTextRange != "undefined") {
            var textRange = document.body.createTextRange();
            textRange.moveToElementText(el);
            textRange.collapse(false);
            textRange.select();
        }
    }

    placeCaretAtEnd( document.getElementById("content") );
user3241019
  • 811
  • 9
  • 20