JSFiddle - React, Tailwind, and code Playground
HTML
<p>
<span id="subject-47" class="subject highlighted">Semencic credits his early familiarity with the breed to his own travels to South Africa<span class="count">4</span></span>, but especially to his frequent correspondence with the head of the first South African Boerboel club, one Mr. Kobus Rust. <strong>The Boerboel Breeders Association was established in 1983</strong> in the Senekal district of the Free State with the sole objective of ennobling and promoting the Boerboel as a unique South African dog breed.
</p>
JavaScript
$(document.body).bind('mouseup', function(e){
//snapSelectionToWord();
var txt = getSelectionParentElement();
class_select = txt.getAttribute("class");
if(class_select !== null && class_select.indexOf("highlighted") > -1) {
alert("highlighted");
return false;
} else {
alert("NO highlighted");
return true;
}
/*
var contentPos = $('.entry-content').html().indexOf( txt );
if( contentPos > 0 ) {
// do my thing here
// after I check txt IS NOT within a .highlighted css class
}
*/
});
function getSelectionParentElement() {
var parentEl = null, sel;
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount) {
parentEl = sel.getRangeAt(0).commonAncestorContainer;
if (parentEl.nodeType != 1) {
parentEl = parentEl.parentNode;
}
}
} else if ( (sel = document.selection) && sel.type != "Control") {
parentEl = sel.createRange().parentElement();
}
return parentEl;
}