Select text within span when clicked
P粉147747637
P粉147747637 2024-03-19 21:47:40
0
1
429

I want to get the text that I clicked on, so if I click on the word "mother" the log will only show the word "mother" even though it is within a span that contains another word,

I used this code but it doesn't even select the span:

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>

P粉147747637
P粉147747637

reply all(1)
P粉851401475

span-split option works in all browsers and does not require the use of third-party libraries.

 
        
    
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template