JSFiddle - React, Tailwind, and code Playground

by Rashit1122233

HTML

<textarea name="name" id="test" cols="30" rows="10"></textarea>
<a href="https://youtu.be/Ft8PT9uA-FY">youtube</a>

JavaScript

var getSelectedText = function() {
    var text = '';
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection) {
        text = document.selection.createRange().text;
    }
    return text;
}

$('#test').on('mouseup', function(){ 
    var text = getSelectedText();
    if (text != ''){
    	//alert(text);
      var zn = $('#test').val();
      $('#test').val(zn + '<a href="https://youtu.be/Ft8PT9uA-FY">youtube</a>');
    }  
});