JSFiddle - React, Tailwind, and code Playground

by Tejash Soni

HTML

<p id="yourTextContainer">asdasda asdasda zazazaa asdasd asdasdsdasd asdasdasd asdasd asd asdas asdasda ad asdasd asd asd asd asd asd asda asdasd a asd as asd asd asd asdsdasdadad asd</p>

JavaScript

$(document).ready(function()
{
    var words=$("#yourTextContainer").text().split(' ');
    $("#yourTextContainer").html("");

    $.each(words, function(i,val)
    {
        //wrap each word in a span tag 
        $('<span/>').text(val +" ").appendTo("#yourTextContainer");
    });

    //$("#yourTextContainer span").live("dbclick",function(event)
    $("#yourTextContainer span").dblclick(function(event) 
    {
        event.stopPropagation();
        res = SelectText($(this));
        alert (res);
        console.log ("sel "+res);
    });
});



function SelectText(element)
{
    var doc = document,
            text = element.get(0),
            range,
            selection;
    if (doc.body.createTextRange)
    {
        range = document.body.createTextRange();
        range.moveToElementText(text);
        range.select();
        return range.toString();
    }
    else if (window.getSelection)
    {
        selection = window.getSelection();
        range = document.createRange();
        range.selectNodeContents(text);
        selection.removeAllRanges();
        selection.addRange(range);
        return selection.toString();
    }
}