Search code examples
jquerycontenteditablerangy

Rangy & ContentEditable - Set the Caret


I'm trying out the latest version of the "rangy" jQuery plugin (1.2 beta) to set the caret in a contenteditable DIV with a specific offset.

However, it responds with a weird error in Firefox: Security error" code: "1000

Here is the offending code:

var el = $("#editablediv"), index = 11;
var range = rangy.createRange();
range.setStart(el, index);
var sel = rangy.getSelection();
sel.setSingleRange(range);

The code fails when calling the setStart function.

Could anyone give an example of the proper usage of rangy please?


Solution

  • I found the issue, I was supposed to pass through the correct node which is the text node:

    var el = $("#editablediv"), index = 11;
    var range = rangy.createRange();
    range.setStart(el[0].childNodes[0], index);
    range.collapse(true);
    var sel = rangy.getSelection();
    sel.setSingleRange(range);