jQuery - Click word to move it to textarea
by jarosciak
HTML
<html>
<head>
<title>jQuery UI Autocomplete - Multiple values</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
</head>
<body>
<div id="topHelp">
<b> <a href="https://en.test.org/" target="_blank">
Newt</a>
</b> - word1 wor2 was born in Woolsthorpe Manor, Woolsthorpe-by-Colsterworth, near Grantham, England... <br><b>Deep Links: </b><br><a href="https://en.test.org/wiki/#Life" target="_blank">Life</a> - Newton was born
</div>
<br>
<textarea id="textarea" style="width:90%;height: 150px;"></textarea>
</body>
</html>
JavaScript
$(document).ready(function(){
//var regex = new RegExp('/<p>([^\<]*?)<\/p>/g');
$("#topHelp").click(function(){
var s = window.getSelection();
s.modify('extend','backward','word');
var b = s.toString();
s.modify('extend','forward','word');
var a = s.toString();
s.modify('move','forward','character');
// alert(b+a);
// $(this).css("background-color","Yellow");
var wordHighligted = " "+b+a;
wordHighligted = wordHighligted.replace(/\s\s+/g,' ');
wordHighligted = wordHighligted.replace(/ \s*$/, "");
console.log("SELECTED: " + wordHighligted);
$("#textarea").append(wordHighligted);
});
});