3

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?

NeilC
  • 1,380
  • 1
  • 17
  • 36

1 Answers1

15

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);
NeilC
  • 1,380
  • 1
  • 17
  • 36
  • 1
    Yes. There were two problems with the original code: first, `setStart()` requires a DOM node rather than a jQuery object, and second, you were probably thinking in terms of character offsets which means you need the text node, as you realized. – Tim Down Jul 25 '11 at 17:59