I am looking to retrieve the specific text that was clicked on. For example, if I click on the word "mother," I want the log to display just that word "mother" even if it is contained within a span with other words.
The code I have tried doesn't seem to 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>