JSFiddle - React, Tailwind, and code Playground
HTML
<p>Pick some text somewhere to make red.</p>
<button id="click">Click for red</button>
<p class='freddy'>I don't care where you pick from as you see but you can't really pick the button. Picks don't work so well if the first character of the pick is a space character and you might be missing spaces for those. You can fix that.</p>
JavaScript
$('#click').click(function() {
var whatBrowser = getIt();
if (whatIsIt == 'notIE' && whatBrowser) {
notIE(whatBrowser);
}
else if (whatIsIt == "isIE"&& whatBrowser) {
isIE(whatBrowser);
};
});
var whatIsIt = "";
function getIt() {
if (window.getSelection) {
whatIsIt = "notIE";
return window.getSelection();
}
else if (document.getSelection) {
whatIsIt = "notIE";
return document.getSelection();
}
else {
var selection = document.selection && document.selection.createRange();
if (selection.text) {
whatIsIt = "isIE";
return selection;
};
return false;
};
return false;
};
function isIE(selection) {
if (selection) {
var selectionContents = selection.text;
if (selectionContents) {
selection.pasteHTML('<span class="reddy">' + selectionContents + '</span>');
};
};
};
function notIE(selection) {
var range = window.getSelection().getRangeAt(0);
var selectionContents = range.extractContents();
var span = document.createElement("span");
span.className= "reddy";
span.appendChild(selectionContents);
range.insertNode(span);
};