JSFiddle - React, Tailwind, and code Playground

by Tejash Soni

HTML

<p id="yourTextContainer">asdasda asdasda 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)
    {
        event.stopPropagation();
        SelectText($(this));
    });
});

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();
    }
    else if (window.getSelection)
    {
        selection = window.getSelection();
        range = document.createRange();
        range.selectNodeContents(text);
        selection.removeAllRanges();
        selection.addRange(range);
    }
}