I want to get the text in which I clicked on, So if I click on word "mother" the log just show this word "mother", even if it's inside an span with another word,
I used this code but it doesn't even select the spans :
function getSelectedText(e) {
if(window.getSelection)
return console.log(window.getSelection().toString());
else if(document.getSelection)
return console.log(document.getSelection());
else if(document.selection)
return console.log(document.selection.createRange().text);
return console.log("");
}
document.body.onmouseup = getSelectedText;
<div class="destination">
<span class="word">sister mother</span>
<span class="word" >brother</span>
<span class="word" >father</span>
</div>
<h1>hi</h1>