Search code examples
javascripttinymce

tinyMCE 4: add Class to selected Element


I created a tinymce menu item and what I want it to do is add a class to the selected text element. I cannot seem to figure out how to do this. Any suggestions? Adding my menu item looks like this:

tinymce.PluginManager.add('button', function(editor, url) {
    editor.addMenuItem('button', {
        icon: '',
        text: 'Button',
        onclick: function() {

            tinyMCE.activeEditor.dom.addClass(tinyMCE.activeEditor.selection, 'test'); //not working
        },
        context: 'insert',
        prependToContext: true
    });
});

I'd be very thankful for any helpful hint.


Solution

  • I found a solution that may not be perfect (for example, if you select part of a text then this doesn't work as I hoped), but for now it does what I want:

    tinyMCE.activeEditor.dom.addClass(tinyMCE.activeEditor.selection.getNode(), 'test');
    

    if I do this on a link, for example, the scripts add the classname "test" to my tag.