Skip to content

Newcomer: programmatically set selection? #267

Myrmex opened this Issue Nov 13, 2012 · 4 comments

2 participants

Myrmex commented Nov 13, 2012

Hi, this lib looks great! Sorry for the stupid question, but I'm a newbie in JS world. I was trying to programmatically set the selection in the editor so that I can apply some formatting to specific words, but I cannot manage to do it. Here is my sample code, could anyone point me in the right direction?

var selection = editor.composer.selection;
var r = selection.getRange();
r.startOffset = 3;
r.endOffset = 6;

I also tried using rangyinput to do the same; it works in a normal textarea, but not here. Sample:

var ta = editor.textarea.element;
$(ta).focus().setSelection(3, 6);


Hi @Myrmex, did you have any success here?

Myrmex commented Jan 16, 2013

I'm sorry I didn't. I looked for several libraries, but it seems that none allows for programmatic selection.


I'm looking at it.. wysihtml5 uses rangy (, and it seems to make it possible.

There is a nice example here:

You can, for example, select a node:


I'm trying to make a selection based on an array-like reference, pointing beginning and end. The range object seems to carry the answer, needs some hacking.


Maybe, this is the path..

Could not implement.
There is a module under development called "rangy-textrange", it adds some extra functions that can help us with this. Will be released at rangy 1.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.