I was trying to add content to the TinyMCE textarea from MySQL. I used setContent(content)
, but the textarea didn't get filled with content on page refresh, and I got this error message below:
Uncaught TypeError: Cannot read property 'setContent' of null
<script src="__PUBLIC__/tinymce/tinymce.min.js"></script>
<script> tinymce.init({selector:'textarea',plugins:'code',content_css:"__PUBLIC__/custom_content.css"});
tinymce.get('textarea').setContent(content....);
</script>
<script src="__PUBLIC__/jquery-1.11.2.min.js"></script>
<textarea class="form-control" id="textarea" name="content" placeholder="content..." rows="30"></textarea>
What am I doing wrong?
You are calling the tinymce.*
functions too early - when the element doesn't even exist in the DOM. Place the <script>
to below the <textarea>
and try again.
Alternatively, wrap your tinymce.*
calls like this (also note the jQuery script placement):
<script src="__PUBLIC__/tinymce/tinymce.min.js"></script>
<script src="__PUBLIC__/jquery-1.11.2.min.js"></script>
<script>
$(document).ready(function() {
tinymce.init({
selector:'textarea',
plugins:'code',
content_css:"__PUBLIC__/custom_content.css"
});
tinymce.get('textarea').setContent(content....);
});
</script>