TinyMCE - adding an ON/OFF toggle switch

TinyMCE - adding an ON/OFF toggle switch

July 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
TinyMCE - adding an ON/OFF toggle switch

Enhancing TinyMCE: Adding an ON/OFF Toggle Switch

TinyMCE is a powerful rich text editor that’s widely used across various platforms, including Magento admin sections. While it provides robust functionality out of the box, sometimes you might want to give users more control over its behavior. In this post, we’ll explore how to add an ON/OFF toggle switch for TinyMCE editors in your Magento admin section using jQuery.

The Current Challenge

You’re currently using version 4.8.3 of TinyMCE with no conflicts mode (noConflict) and have the editor initialized on all textarea elements. However, you’ve encountered a problem when trying to add an ON/OFF toggle switch. Here’s what’s happening:

  1. Your current solution only affects the first instance of TinyMCE on the page.
  2. Any additional instances are not being disabled or re-enabled by your script.

Let’s dive into the code and understand where things might be going wrong.

Analyzing the Current Code

You’ve shared a snippet that leverages jQuery to add an ON/OFF toggle switch below each textarea element. Here’s a breakdown of what it does:

var $j = jQuery.noConflict();
$j(function() {
    $j('textarea').after("<br/><span class=\"toggle form-button\">Toggle TinyMCE</span>");
    $j("span.toggle").toggle(
        function() {
            $j('.wrapper').find('textarea').tinymce().hide();
        },
        function () {
            $j('.wrapper').find('textarea').tinymce().show();
        }
    );
});

The script accomplishes the following:

  1. It initializes a noConflict mode for jQuery.
  2. When the document is ready, it selects all textarea elements and appends an ON/OFF toggle switch span after each one.
  3. The toggle function is attached to the span, which hides and shows the TinyMCE editor based on the button’s state (OFF/ON).

The Missing Piece: Selector Specificity

The key issue lies in the selector you’re using to target the TinyMCE instances:

$j('.wrapper').find('textarea')

While this will work for the first instance, it may not properly select other TinyMCE editors if they are nested within different containers. To ensure all instances are targeted correctly, consider using a more precise selector or class name.

A Better Approach: Using Unique Class Names

To accurately target all TinyMCE instances while maintaining your existing code structure, you can:

  1. Add a unique class to each textarea element that requires the editor.
  2. Modify your jQuery selector accordingly.

Here’s an updated example:

First, modify your HTML markup:

<textarea class="tiny-mce-editor">Your content goes here.</textarea>

Then, update your JavaScript code as follows:

var $j = jQuery.noConflict();
$j(function() {
    $j('textarea.tiny-mce-editor').after("<br/><span class=\"toggle form-button\">Toggle TinyMCE</span>");
    $j("span.toggle").toggle(
        function() {
            $j(this).prevAll('.tiny-mce-editor').tinymce().hide();
        },
        function () {
            $j(this).prevAll('.tiny-mce-editor').tinymce().show();
        }
    );
});

In this updated snippet, we’re targeting only the textarea elements with class tiny-mce-editor. The this keyword inside the toggle functions refers to the current span element being clicked.

Why This Works

By using a unique class name for textareas that require TinyMCE initialization and updating your jQuery selector accordingly, you ensure that all instances are properly targeted. The prevAll('.tiny-mce-editor') selector picks up only the textarea elements preceding the clicked toggle button, which guarantees that each TinyMCE instance is disabled or re-enabled correctly.

Conclusion

Adding an ON/OFF toggle switch for your TinyMCE editors in Magento admin involves carefully crafting jQuery selectors to target all instances accurately. By using unique class names and updating your script accordingly, you can create a robust solution that gives users the control they need over the rich text editor’s behavior.

Remember to consult the official TinyMCE documentation for more advanced configurations and best practices: https://www.tiny.cloud/docs/advanced/tinymce-and-jquery/

As always, feel free to reach out if you have any further questions or need additional assistance. Happy coding!

Database Admin Errors JavaScript

Our network