22
votes

Hi I need to set predefined content inside the tinyMCE Editor. Below is my html and jquery.

<script type="text/javascript">
    tinyMCE.init( {
        mode : "exact" ,
        elements : "country"
    });
</script>
<script type="text/javascript">
    $(function() {
        $("#lang").change(function() {
            var s = $(this).val(); alert(s);
            $("#country").val(s);
        })
    })
</script>


<select id="lang">
        <option value="">Please Select country</option>
        <option value="us">US</option>
        <option value="es">SPAIN</option>
        <option value="jp">JAPAN</option>
    </select><br /><br />
    <textarea id="country" cols="10" rows="5"></textarea>

The script works for a normal textarea but not for tinyMCE. Is there anything I am doing wrong in this.

Thanks

5
Im pretty sure tiny mce renders a sudo element for users to type into so if your change the value of country it will not change the output. However there is a special plugin for tinymce to interact with jquery tinymce.com/tryit/jquery_plugin.phpDominic Green

5 Answers

40
votes

I think you can do:

$(function() {
    $("#lang").change(function() {
        var s = $(this).val(); 
        alert(s);
        tinyMCE.activeEditor.setContent(s);
    });
});
14
votes

For me only that's code works :

tinyMCE.get('my_textarea_id').setContent(my_value_to_set);

Maybe this is the code from the new version of tinyMCE ! (Tiny MCE Api 3)

3
votes

Simply this works for me

$("#description").val(content);

1
votes

You can also try this:
Without Replace whole content inside tinymce, set cursor where you want add value inside tinymce

$(document).on('change','#lang', function() {
     var Getname = $(this).val();
     if (Getname != '') {
        //tinyMCE.activeEditor.setContent(s);   // This is for replace all content
        tinyMCE.activeEditor.execCommand('mceInsertContent',false,Getname); // Append new value where your Cursor
        //console.log(Getname)
    }
 });

I am using this code the new version of TinyMCE 5

0
votes

Before an ajax call, you need to call a trigger

tinyMCE.trigge*emphasized text*rSave(true, true);

Full Syntax

  tinyMCE.triggerSave(true, true);
        $.ajax({
          data: $('#userForm').serialize(),

          url: "{{ route('versions.store') }}",
          type: "POST",
          dataType: 'json',
          success: function (data) {
          }
          });