WordPress tinymce insert text

I have problem with tinymce in wordpress. I need to put inside tinymce element . In tinymce editor where are two tabs “Visual” and “HTML”. When I use my code in “HTML” its works. But when I want to use in “Visual” its not working…

I was trying like this:

Read More

JavaScript

function insert_tag(text){
    var cont = document.getElementById('bbp_topic_content');
    cont.value += '<div>' + text + '</div>';
}

HTML

<a onclick="insert_tag('HELLO')" >Insert text</a>

Any help please ?

Related posts

Leave a Reply

1 comment

  1. You may try this

    function insert_tag(text){
        if(tinyMCE && tinyMCE.activeEditor)
        {
            tinyMCE.activeEditor.selection.setContent('<div>' + text + '</div>');
        }
        return false;
    }
    

    Also use return in your a tag

    <a onclick="return insert_tag('HELLO')" >Insert text</a>
    

    Reference.