<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<textarea id="mytextarea">
<p>
I am editable.
</p>
<p>
<span class="mceNonEditable">
I am not editable, but I want to be wrapped by a
bold tag when the bold button is clicked.
</span>
</p>
</textarea>
<br/>
<br/> When I click on the <b>B</b> button while the non editable span is selected in the editor, I would like the non editable span to be wrapped by a <b>b</b> tag.
JavaScript
tinymce.init({
selector: '#mytextarea',
plugins: 'textcolor noneditable',
toolbar: 'forecolor backcolor | bold italic underline | variables',
menubar: false,
statusbar: false,
// Tells Tinymce to treat any content with [] around it as noneditable
noneditable_regexp: [/\[[^\[\]]*\]/g],
setup: function (editor) {
var $ = tinymce.dom.DomQuery;
var nonEditableClass = editor.getParam('noneditable_noneditable_class', 'mceNonEditable');
// Register a event before certain commands run that will turn contenteditable off temporarilly on noneditable fields
editor.on('BeforeExecCommand', function (e) {
// The commands we want to permit formatting noneditable items for
var textFormatCommands = [
'mceToggleFormat',
'mceApplyTextcolor',
'mceRemoveTextcolor'
];
if (textFormatCommands.indexOf(e.command) !== -1) {
// Find all elements in the editor body that have the noneditable class on them
// and turn contenteditable off
$(editor.getBody()).find('.' + nonEditableClass).attr('contenteditable', null);
}
});
// Turn the contenteditable attribute back to false after the command has executed
editor.on('ExecCommand', function (e) {
// Find all elements in the editor body that have the noneditable class on them
// and turn contenteditable back to false
$(editor.getBody()).find('.' + nonEditableClass).attr('contenteditable', false);
});
// Register a button to add noneditable variables to the content
editor.addButton('variables', {
type: 'listbox',
text: 'Variables',
icon: false,
values: [
{ text: 'First Name', value: 'first_name' },
{ text: 'Last Name', value: 'last_name' }
],
onselect: function (e) {
editor.insertContent('[' + this.value() + ']');
}
});
},
init_instance_callback: function (editor) {
/*
The following two hacks fix some weirdness with...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.