Search code examples
jqueryhtmlfroala

Froala editor: insert into caret position when clicking a div


I'm using Froala v2.6.1 and I want to insert a string into the editor exactly at the last caret position when user click a divs, but the string is always inserted at the end of the editor.

Here is the thing I did:

<div class="variable" data-value="{{user_id}}">USER ID</div>

Jquery:

$('div.variable').click(function() {
    $("#template_editor").froalaEditor('html.insert', $(this).data('value'), true); 
});

Anyone know how to solve this would be great help.


Solution

  • It looks you have an external button there. For that the editor has a built-in mechanism, highlighted in a demo. In your case it would be something like this:

    $('#template_editor')
      .on('froalaEditor.initialized', function (e, editor) {
        editor.events.bindClick($('body'), 'div.variable', function (ev) {
          var that = ev.originalEvent && ev.originalEvent.originalTarget;
          editor.html.insert($(that).data('value'));
        });
      })
      .froalaEditor()