I've the following div:
<div id="target" >hello(first), hello(second), hello(third), hello(fourth)</div>
and following code based on this discussion:
$(document).ready(function(){
$('#target').bind('mouseup', function(){
var needle = window
.getSelection()
.getRangeAt(0);
var haystack = $(this).text();
var newText = haystack.replace(needle,'<span class="highlight">' + needle + '</span>');
$(this).html(newText);
});
When I select one of the "hello", it 'randomly' highlights one of them rather than the actual selected "hello".
How can I highlight the selected one?
Thanks in advance for your help.