I'm looking for a way to programatically select all the content inside a TinyMCE editor instance.
The reason I need this is that I'd like it if all the text inside the editor was selected, as soon as someone clicks on it (I'm using TinyMCE in conjunction with JEditable, by the way).
Thanks,
Edan
The TinyMCE getContent and setContent methods You can do this using the getContent() API method. Let's say you have initialized the editor on a textarea with id=”myTextarea”. This will return the content in the editor marked up as HTML.
Here is the code that will replace your editor content. But you will need to do this action at the correct time. var editor = tinyMCE. get('my_editor_id'); also works.
Re: How refresh editor after setContent Well, both exec mceInsertContent and your workaround (do a selection. setContent then another editor. setContent) basically do the same thing. tinymce.
You can do this using the getContent () method from the TinyMCE API. Let’s say you have initialized the editor on a textarea with id=”myTextarea”. First access the editor using that same id, then call getContent ().
After the TinyMCE editor is added to the initial example, the save and load functions are updated to access the editor content using these methods. Often you will want the editor populated with content as soon as it’s initialized.
Create setdata.html file inside the tinymce project folder. Create setdata.js file inside the js folder. This file will contain the javascript code that we are going to write. Now the project folder will look like the following...
Or, instead of accessing the editor by id, you can access the active editor. When you’re using TinyMCE in your apps for users to edit content previously stored somewhere else, like in a CMS, you’ll need a way to populate the editor with that content. You can do this using the setContent () method from the TinyMCE API.
Assuming you have your TinyMCE editor instance stored in a variable called ed
:
ed.selection.select(ed.getBody(), true);
For tinyMCE 4 I'm using the Range Object for selection:
function selectAll(editor) {
range = editor.dom.createRng();
range.selectNodeContents(editor.getBody());
editor.selection.setRng(range);
}
selectAll(tinyMCE.focusedEditor);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With