简体   繁体   中英

Set Value Inside a TinyMCE Editor using jQuery

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

I think you can do:

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

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)

Simply this works for me

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

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

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) {
          }
          });

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