Tony Bogdanov
Tony Bogdanov

Reputation: 7686

TinyMCE: How to insert content and select it?

How does one insert, or better, replace the current selection with some content and then select it?

Here's my text: Hello nice world!

As you can see nice is selected by the user. Now he clicks a button and this code is run:

editor.execCommand('mceReplaceContent', 'nasty');

This works just fine, the result is: Hello nasty world, but nothing is selected.

How do I make it automatically select nasty in the result content?

This seems like a very natural thing for one to want to do, but can't seem to find a straight-forward solution. I need this to work in mostly two cases 1) I am wrapping the selected text in a f.e. span element or 2) I am removing the wrapping span element.

I know there are better ways of dealing with nodes, but I'm more concerned about the pure text scenario right now.

Thanks in advance!

P.S. I am using TinyMCE 3 not 4.

Upvotes: 1

Views: 3206

Answers (1)

A1rPun
A1rPun

Reputation: 16837

I found this in the docs (API 3.x)

// Sets some contents to the current selection in the editor
tinyMCE.activeEditor.selection.setContent('Some contents');

// Selects the first paragraph found
tinyMCE.activeEditor.selection.select(tinyMCE.activeEditor.dom.select('p')[0]);

The setContent function does practically the same as execCommand('mceReplaceContent'). I did not found something like the easy DOM properties selectionStart & selectionEnd.

Upvotes: 2

Related Questions