Insert text at user clicked postion

by Santosh J

HTML

<div class="sentence">This is a sentence</div>

JavaScript

$('.sentence').on('click', function (e) {
    var to_insert = 'test';
    console.log(window.getSelection().anchorOffset, $(this).html(), window.getSelection())
    // determine exactly where inside the div I clicked
    // insert 'to_insert' at that location
    $(this).html($(this).html().substring(0, window.getSelection().anchorOffset) + to_insert + $(this).html().substring(window.getSelection().anchorOffset));
});