Establecer el valor de textarea con javascript después de inicializar TinyMCE


Tengo un área de texto y estoy usando TinyMCE en esa área de texto.

Lo que estoy haciendo en realidad es que cuando se abre la página, estoy llenando el área de texto con algo de texto, y después de eso estoy inicializando el TinyMCE.

El problema es cuando estoy tratando de cambiar el valor de la textarea después de la inicialización de TinyMCE, entonces no sucede nada.

Aquí hay un ejemplo.

  1. Creando el área de texto:

    <textarea style="width: 95%;" name="title"  id="title"></textarea>
    
  2. Poblando el área de texto:

    $('#title').html("someText");
    
  3. Inicializando TinyMCE

    tinyMCE.init({
            // General options
            mode : "specific_textareas",
            theme : "advanced",
            width: "100%",
            plugins : "pagebreak,paste,fullscreen,visualchars",
    
            // Theme options
            theme_advanced_buttons1 : "code,|,bold,italic,underline,|,sub,sup,|,charmap,|,fullscreen,|,bullist,numlist,|,pasteword",
            theme_advanced_buttons2 :"",
            theme_advanced_buttons3 :"",
            theme_advanced_buttons4 :"",
            theme_advanced_toolbar_location : "top",
            theme_advanced_toolbar_align : "left",
            theme_advanced_statusbar_location : "bottom",
            valid_elements : "i,sub,sup",
            invalid_elements : "p, script",
            editor_deselector : "mceOthers"
        });
    
  4. Me gustaría cambiar el contenido de textview (pero no funciona)

He intentado usar lo mismo que antes en el TinyMCE

    $('#title').html("someModifiedText"); // does not work

También he intentado eliminar TinyMCE:

    if(tinyMCE.getInstanceById('title'))
    removeTinyMCE("title");

Con

function removeTinyMCE (dialogName) {
    tinyMCE.execCommand('mceFocus', false, dialogName);
    tinyMCE.execCommand('mceRemoveControl', false, dialogName);

}

Y thet para reutilizar:

    $('#title').html("someModifiedText"); // does not work

Me he quedado sin ideas... Muchas gracias por su ayuda....

Author: Thariama, 2012-08-03

4 answers

El problema aquí es que no verá nada si ingresa texto o html en su área de texto. Su área de texto se oculta cuando tinymce se inicializa. Lo que ves entonces es un iframe editable, que se usa para editar y dar estilo al contenido. Hay varios eventos que harán que tinymce escriba su contenido en el elemento fuente html del editor (en su caso, su área de texto).

Si desea establecer el contenido del editor (que es visible) tendrá que llamar a algo como

tinymce.get('title').setContent('<p>This is my new content!</p>');

También puede acceder a los elementos dom directamente usando lo siguiente

tinymce.get('title').getBody().innerHTML = '<p>This is my new content!</p>';

O usando jQuery

$(tinymce.get('title').getBody()).html('<p>This is my new content!</p>');
 29
Author: Thariama,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-09 09:00:36

Puede usar el tinyMCE.activeEditor.setContent('<span>some</span> html');

Compruebe esta respuesta

 9
Author: Ryan Monreal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-21 00:41:41

Simplemente esto funciona para mí

$("#description").val(content);
 3
Author: AtanuCSE,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-27 11:54:24

Funciona para mí. Simplemente colóquelo dentro de su código html en lugar de ir tinymce

    <textarea> html CONTENT</textarea>
 -1
Author: user2556716,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-10 00:57:08