JSFiddle - React, Tailwind, and code Playground
HTML
<a href="#">Some text <b>and more text</b> and more</a>
<input type="button" onmousedown="var sel = getSelectionTextAndContainerElement(); alert(sel.containerElement.tagName + ',' + sel.text)" value="get selection">
JavaScript
function getSelectionTextAndContainerElement() {
var text = "", containerElement = null;
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var node = sel.getRangeAt(0).commonAncestorContainer;
containerElement = node.nodeType == 1 ? node : node.parentNode;
text = sel.toString();
}
} else if (typeof document.selection != "undefined" &&
document.selection.type != "Control") {
var textRange = document.selection.createRange();
containerElement = textRange.parentElement();
text = textRange.text;
}
return {
text: text,
containerElement: containerElement
};
}