Add or Append text in p:textEditor

Viewed 307

I have p:textEditor like the following

<p:textEditor 
                id="editor"
                widgetVar="editor"
                value="#{xxxController.editorText}" 
                height="300" 
                placeholder="Enter your content"
                toolbarVisible="false"/>

Have the following command button to add/append values to p:textEditor

<p:commandButton onclick="insertTag('[myValue]')" value="myValue" type="button" />

JavaScript
<script>
             
        function insertTag( t )
        {
            PF( 'editor' ).insertText( t ) ;
        }
        </script>

But i get SCRIPT5007: Unable to get property 'insertText' of undefined or null reference when i try to click the <p:commandButton.

So how do we insert/append text using widgetVar or JavaScript to p:textEditor?

Version details

JSF 2.2, PrimeFaces 6.2

2 Answers

See Using the component ID as widgetVar name

[...] This will cause all original widget var functions to be completely unavailable because the variable editor is now referencing a HTMLDivElement instance which doesn't have the same functions as the original widget var like show(), etc. [...]

(Append a _vw to every widgetvar name, to eliminate that issue)

SCRIPT5007: Unable to get property 'insertText' of undefined or null reference

guess, that's exactly the cause, your code is not referencing the "editor"-widgetVar, but the "editor" html element, which does not know "insertText".

Related