2
votes

How to alert the selected text from the tinyMCE editor . I already created a plugin for Moodle tinyMCE. When I click the plugin button in tinyMCE a popup will display. In that popup I want to display the selected text. This is my requirement. For this i want to get the selected text from editor.

What I tried:

alert(tinyMCEPopup.editor.selection.getContent({format : 'text'}));

Nothing is happened

alert(tinyMCEPopup.editor.getContent({format : 'text'}));

The above code alerts the entire text in editor.

I can't get the selected text.I only need the selected text.

I am using Moodle 2.7

1

1 Answers

1
votes

You need to use the current active editor object and then call selection.getContent().

Here is simple TinyMCE plugin to alert the selected text:

(function () {
    tinymce.create('tinymce.plugins.myselectPlugin', {
        init: function (ed, url) {
            ed.addCommand('mceMyselect', function () {
                alert(ed.selection.getContent({format : "text"}));
            });

            // Register button
            ed.addButton('myselect', {
                title: 'myselect.desc',
                cmd: 'mceMyselect',
                image: url + '/img/example.gif'
            });
        },
        createControl: function (name, cc) {
            return null;
        },
        getInfo: function () {
            return {
                longname: 'myselect plugin',
                author: 'Mohamed Alsharaf',
                authorurl: '',
                infourl: 'http://docs.moodle.org/en/TinyMCE',
                version: "1.0"
            };
        }
    });

    // Register plugin.
    tinymce.PluginManager.add('myselect', tinymce.plugins.myselectPlugin);
})();