JSFiddle - React, Tailwind, and code Playground
by bingjie2680
HTML
<span>this is a test, your text goes here, this is a test, your text goes here.</span>
<hr>
<textarea>
this is a test, your text goes here, this is a test, your text goes here.
</textarea>
JavaScript
$.fn.selectRange = function(start, end) {
if(!end) end = start;
return this.each(function() {
if (this.setSelectionRange) {
this.focus();
this.setSelectionRange(start, end);
} else if (this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
}
});
};
$("span").click(function () {
var selObj = window.getSelection();
$("textarea").selectRange(selObj.baseOffset);
});