Set caret position in contenteditable div layer in Chrome/webkit

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2010-04-13T22:33:07Z Indexed on 2010/04/13 22:43 UTC
Read the original article Hit count: 919

Filed under:
|
|
|
|

Hello,
I'm trying to set the caret position in a contenteditable div layer, and after a bit of searching the web and experimenting I got it to work in firefox using this:

function set(element,position){
    element.focus();
    var range= window.getSelection().getRangeAt(0);
    range.setStart(element.firstChild,position);
    range.setEnd(element.firstChild,position);
}

[...]

set(document.getElementById("test"),3);

But in Chrome/webkit it selects all of the content in the div. Is this a bug with webkit or am I doing something wrong?
Thank you in advance.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about caret