Set Value Inside a TinyMCE Editor using jQuery

Mike picture Mike · Dec 20, 2011 · Viewed 43.3k times · Source

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

Answer

karim79 picture karim79 · Dec 20, 2011

I think you can do:

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