Suppose I have a div
that contains a sentence such as the one listed below:
<div class="sentence">This is a sentence</div>
I am trying to create a function/event handler that will recognize when the user clicks on the text inside the <div>
. But, when they click inside the <div>
the function needs to know exactly where they clicked so I can insert a <span>
at that location. For example, if the user clicks in between the letters 'n' and 't' in the word 'sentence', then I want to insert a <span>
there so I end up with
<div class="sentence">This is a sen<span>test</span>tence</div>
Something like this:
$('.sentence').on('click', function (e) {
var to_insert = '<span>test</span>';
// determine exactly where inside the div I clicked
// insert 'to_insert' at that location
});
Any ideas?