7

I have a TinyMCE textarea inside of #container

When I use $('#container').hide() and then $('#container').show(), tinyMCE throws:

Cannot read property 'selection' of undefined

I'm using the jquery plugin, so this is how I set it up:

$('#container textarea').tinymce({ /* options */ });

What should I be doing differently?

4 Answers 4

11

The correct command to use here is

// editor_id is the id of your textarea and 
// tinymce will use this id to uniquely identify this editor instance
editor_id = $("#container textarea").attr('id');
tinymce.get(editor_id).hide();  

to make it visible again use

tinymce.get(editor_id).show();
5
  • I'm still getting Cannot read property 'selection' of undefined (infinitely many times until I refresh the page) when I try to hide using tinymce.get('textareaIDwithoutPoundSign').hide() May 25, 2011 at 14:01
  • if you have only one editorinstance at your page you may use tinymce.editors[0].hide(); too. what happens?
    – Thariama
    May 25, 2011 at 14:39
  • same deal (Cannot read property 'selection' of undefined), though when I try to tinymce.editors[0].show(); it, I get Uncaught TypeError: Cannot read property 'createRange' of undefined. Also I'd really prefer to use the jQuery interface if at all possible May 25, 2011 at 14:45
  • To clarify: I'm using tinymce.editors[0].hide(); and then $('#container-with-tinyMCE-in-it').hide(); to hide the container after hiding tinyMCE (it has other stuff in it) May 25, 2011 at 15:47
  • For me, this only removed the TinyMCE editor instance and required an extra call to hide the textarea itself. Using the same method to show() it again, the extra call to show the textarea was not needed. +1, this works for me
    – Mog
    May 25, 2011 at 19:22
1

This question is about hiding and showing tinymce editor but if anyone came here about removing and re-adding tinymce editor without error then my solution can work for them.

To remove existing tinymce editor and add new needs clearance of tinymce.EditorManager.editors array. This solution works in both cases : 1. If you have only one editor and you want to remove and add it again. 2. If you have multiple editors and you want to remove some special editor and add it again.

console.log(tinymce.EditorManager.editors);

This will give you a view of the array and exact index of you desired editor which you want to remove. For example one sample output of above console can be:

Array[2]
0:B
1:B
length:2
textarea-1:B
textarea-2:B
_proto_Array[0]

This is the output of the console when i have two tinymce editors on textareas : #textarea-1 and #textarea-2 Lets suppose I want to delete #textarea-2 and re-add it then it can be done as follows:

tinymce.EditorManager.editors.splice(1, 1);//removing second element in array.
delete tinymce.EditorManager.editors['textarea-2'];//deleting respective textarea id from array

Then you can add it again simply using init:

tinymce.init({
    selector:'#ts-textarea-2'
});

If you have only one textarea associated with tinymce editor lets say : #textarea-1 and you want to remove and re-initialize it then you can just empty tinymce.EditorManager.editors by :

tinymce.EditorManager.editors = [];

And then you can add using init command as explained above. Worked for me without any error.

I hope it helps

0

Instead of hiding it, try sending it off screen - something like:

$('#container').css('left', '-1000px');

EDIT/UPDATE:

You could also try removing TinyMCE from the textarea before you hide() the container, and then bring it back after you show(). But you'll need to give your textarea an #ID:

//To Enable
tinyMCE.execCommand('mceAddControl', false, $("#container textarea").attr('id'));
//To Disable
tinyMCE.execCommand('mceRemoveControl', false, $("#container textarea").attr('id'));
6
  • @chris: Does that work on IE because if I remember correctly it doesn't like negative px's May 24, 2011 at 18:23
  • Also: I'm using jQuery's animations; so it'd be nice to use those still, rather than it just being gone May 24, 2011 at 18:43
  • Is there a way to do that through the jQuery plugin? (for instance $('#my:textarea').tinymce('disable');). I've tried disable, but I still get the exception. I'd really prefer to handle this through the jQuery plugin May 24, 2011 at 19:58
  • Try this: $('#container textarea').tinymce().execCommand('mceRemoveControl', false, $("#container textarea").attr('id')); May 24, 2011 at 21:36
  • Is there a way to do that through the jQuery plugin? May 25, 2011 at 14:25
0

Apparently it was the animation. if I show()/hide() I'm fine, but when I try to animate in tinyMCE has an issue after I finish animating, possibly trying to set options once the textarea's display isn't none.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.