How to access the ckeditor function within a dialog HTML element without relying on the "onOk" event

I'm in the process of developing my own plugin and dialog using an html element. I have a requirement where upon clicking the html element, I need to add some text to the editor. However, I'm facing challenges with the onOk function and finding it difficult to bypass it.

If I include editor.insertHtml(' some code ') within the onOk function, the text is successfully added. But when I try to execute it outside, I encounter an error saying

Uncaught TypeError: Cannot read property 'editor' of undefined(…)
.

Can someone guide me on the correct approach to access the editor?

CKEDITOR.dialog.add( 'smiley2', function( editor ) {
    return {
        title: 'Abbreviation Properties',
        minWidth: 400,
        minHeight: 200,
        contents: [
            {
                id: 'tab-basic',
                label: 'Basic Settings',
                elements: [
                    {
                        type: 'html',
                        id: '2',
                        label: 'Explanation',
                        html: "<div onclick=\"editor.insertHtml(' some code ')\">add code</a></div></div>"
                    }
                ]
            }
        ],
         onShow : function()
            {          
            document.getElementById(this.getButton('ok').domId).style.display='none'; // hide ok button
            },
        onOk: function() {

            editor.insertHtml(' abbr ');
        }
    };
});

Answer №1

CKEDITOR.dialog.add( 'emotion', function( editor ) {

    var onSelection = function(event) {
            var selectedElement;
            selectedElement = new CKEDITOR.dom.element(event); 
            editor.insertHtml("EMOTIONAL RESPONSE ADDED!");
            CKEDITOR.dialog.getCurrent().hide();
    };
    var onClickEvent = CKEDITOR.tools.addFunction(onSelection); 

    return {
        title: 'Emotion Properties',
        minWidth: 400,
        minHeight: 200,
        buttons: [CKEDITOR.dialog.cancelButton],
        contents: [
            {
                id: 'tab-basic',
                label: 'Basic Settings',
                elements: [
                    {
                        type: 'html',
                        id: '2',
                        label: 'Explanation',
                        html: "<div onclick=\"CKEDITOR.tools.callFunction(" + onClickEvent +", this); return false;\" >Click here to add an emotional response</a></div>"
                    }
                ]
            }
        ] 

    };
});

Answer №2

Once you click on the division element within your example, the writer won't be within your reach. You must acquire your writer entity and utilize its insertContent function in this manner;

CKEDITOR.instances.writer1.insertContent('your content`)

In this scenario, it is assumed that your textbox identifier is set as writer1.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigate to the top of a Bootstrap accordion when it is expanded

Recently, I've been working on a small blog and I want to make consecutive blog posts accessible from within an accordion. This will allow users to easily skim through post titles, select the one they find interesting, read it, and seamlessly go back ...

This module is not compatible with the "node" engine when trying to install React-chartjs-2 Chart.js

While working on a new project, I successfully implemented a doughnut chart using chart.js and its react wrapper. However, when attempting to install it in the main project, I encountered some difficulties. Here is an example of the new project where the ...

The key length specified in crypto.createCipheriv is not valid

After utilizing NodeJS v8.11.0, I managed to create a base64-encoded key with the following code: const secret = 'shezhuansauce'; const key = crypto.createHash('sha256').update(String(secret)).digest('base64'); //output is RE ...

What other methods are available to verify null and assign a value?

Is there a more efficient approach for accomplishing this task? theTitle = responsesToUse[i]["Title"]; if(theTitle == null) theTitle = ""; ...

Designing a nested function within a function encapsulated within a class

Suppose I have a class with a function inside: var myClass = class MyClass() { constructor() {} myFunction(myObj) { function innerFunction() { return JSON.stringify(myObj, null, 2); } return myObj; } } In this scenario, callin ...

What is the reason why performance.now() does not return accurate results when used in a synchronous callback function in Node.js?

Trying to implement an asynchronous read-write operation in Node.js and measuring the execution time using performance.now has presented a challenge. It appears that within the callback function, performance.now returns undefined. const { performance } = r ...

The canvas is giving Three.js trouble when it comes to rendering

I included the script in my index.html file, but when I view the page, it displays as rendered without any content inside. I'm unable to locate the issue. Below is the code from the external .js file: var wdt = $('.design').width(); va ...

What makes a function in JavaScript special as both a constructor and an object?

After conducting extensive research on this topic, I have not been able to find a definitive answer. According to one source, when the JavaScript engine encounters a function statement, it creates a new Function() object, leading me to believe that it coul ...

Move the cursor to a specific position within a contentEditable <div> element

I am in search of a reliable solution that works seamlessly across browsers to set the cursor/caret position to the last known location when a contentEditable='on' <div> regains focus. By default, the cursor/caret in a content editable div ...

Searching for the position of objects within a collection?

I am seeking the ability to parse through an object and allocate each of its available attributes to a variable. Within this scenario, there are four potential properties present in different objects - some containing all four while others may only have t ...

Using AngularJS to handle click events and blur events

I am attempting to dynamically change the class based on user interaction in my code. The class should switch between "large" and "active" depending on whether the input field is filled or not. Please see the modified script below. <div class="snippe ...

Speedy Guide to Referencing Variables in JavaScript

I'm hoping for some clarification on this topic. Let's imagine I have 2 global variables: var myarray=[1,3,5,7,9],hold; and then I execute the following code: function setup() { alert (myarray[0]);//displays 1 hold=myarray; alert (hold);//seem ...

Is it possible to incorporate custom meta tags into next.js 13 using static metadata objects?

I'm trying to block Pinterest on my website by adding the meta tag <meta name="pinterest" content="nopin" /> in the site's header, but I'm uncertain how to do this in Next 13 using the new exported metadata object f ...

Ensure that an element exists in Selenium before proceeding with actions

Currently, I am undergoing testing with the Selenium web driver and one of the requirements is to wait for my object to be defined on the page. Important: There are no visible changes in the DOM to indicate when my object is ready, and it's not feasib ...

JavaScript code can be used to access data from a JSON file and present the flower and color information in separate unordered lists

How can I retrieve data from a JSON file in JavaScript and display flowers and colors in separate unordered lists? I am having trouble adding it. fetch('dataFiles/midterm.json', { method: 'GET', mode: 'no-cors' ...

What is the best way to utilize JSONP to display an entire HTML code with an alert?

When I attempt to use cross-domain ajax to retrieve the entire HTML page code, I keep receiving a failed message. The console log shows: "Uncaught SyntaxError: Unexpected token <" Below is my AJAX function: function fetchData(){ var url = documen ...

The error message "TypeError: Cannot access 'url' property of undefined in Strapi

I am facing an issue with a list of items where some have download links while others do not. Whenever I try to render an undefined URL, it triggers an error. To resolve this, I attempted the following: if (spectacle.pdf.url) { const pdf = spectacle.p ...

Instead of using setTimeout in useEffect to wait for props, opt for an alternative

Looking for a more efficient alternative to using setTimeout in conjunction with props and the useEffect() hook. Currently, the code is functional: const sessionCookie = getCookie('_session'); const { verifiedEmail } = props.credentials; const [l ...

What is the most effective way to retrieve emoji images in a chat using DiscordJS?

I am aiming to create a function that allows me to run a simple command like $yoink. This command would essentially convert the last sent emoji in the chat into an image that I can then download. I have a similar functionality already set up, however, it c ...

Guidelines for submitting on button click within MaterialUI stepper

I am currently developing a Register Form utilizing MaterialUI stepper. Each step in the form corresponds to a specific component: const steps = ['Basic information', 'Profile', 'Review registration'] At the final step of the ...