Set Value Inside a TinyMCE Editor using jQuery
Asked Answered
B

5

25

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

Bartlet answered 20/12, 2011 at 13:17 Comment(1)
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.phpApproval
C
44

I think you can do:

$(function() {
    $("#lang").change(function() {
        var s = $(this).val(); 
        alert(s);
        tinyMCE.activeEditor.setContent(s);
    });
});
Coburn answered 20/12, 2011 at 13:21 Comment(3)
For a specific tinyMCE instance, one can also use tinyMCE.getInstanceById('textarea_id').setContent(s);Cressler
It's working great. I am using it as ajax response to set in the tinyMCE.Occur
With the latest TinyMCE version (5) you need this code: tinymce.get('elementid').setContent(s); You can learn more in the API docs: tiny.cloud/docs/api/tinymce/tinymce.editor/#setcontentGesellschaft
C
16

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)

Coseismal answered 23/8, 2015 at 17:58 Comment(0)
S
5

Simply this works for me

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

Shoot answered 27/9, 2013 at 11:51 Comment(0)
K
1

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

Kliment answered 25/4, 2020 at 22:11 Comment(0)
N
0

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) {
          }
          });
Nolpros answered 7/6, 2021 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.