简体   繁体   中英

Tinymce.get and tinymce.activeEditor both return null

I have a tinymce textarea in HTML: <textarea id="customer_notes"></textarea>

And in JS I try to access this textarea with

tinymce.get('customer_notes').on("keyup", function(){ console.log("tiny"); })

OR

tinymce.activeEditor.on('keyup', function(){ console.log("keyup"); })

However, both are returning null. I understand that there can be an issue with the element not being loaded on the page yet, and therefore is inaccessible.

BUT just above either statement I try, if I output console.log(tinymce.editors); it outputs an array containing my textarea, with the id "customer_notes".

0: {…}, id: "customer_notes", isNotDirty: true, plugins: {…}, …}ustomer_notes: eN { id: "customer_notes", isNotDirty: true, plugins: {…}, …} length: 1 __proto__: Array(0)

I have read several other posts with this issue, but none of the solutions seem to work for me. One answer suggested using tinymce.editors[0] but that is undefined?

Thanks in advance!

Your code seems to work fine. If you can share all your HTML and JS then maybe we can pinpoint where the issue is. In the meantime, here's a working example to hopefully help you along: JSFiddle .

HTML:

<!DOCTYPE html>
<html>
<head>
  <script src="https://cloud.tinymce.com/5/tinymce.min.js"></script>
</head>
<body>
  <textarea id="customer_notes"></textarea>
</body>
</html>

JS:

$(function(){
  tinymce.init({ selector:'textarea' });

  tinymce.get('customer_notes').on("keyup", function(){
    console.log("tiny");
  });

  tinymce.activeEditor.on('keyup', function(){
    console.log("keyup");
  });
});

If you want to capture events regarding an instance of TinyMCE you can actually setup these event functions directly in your init({}) function.

Here is an example:

http://fiddle.tinymce.com/7Hgaab

You will see there is a setup function in the init and that setup function automatically gets access to your editor object so you can setup your keyup event function in that setup function.

The code to look at in the init({}) is:

setup: function (editor) {
  editor.on('keyup', function (e) {
    console.log('A key was pressed: ', e.keyCode); 
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM