JSFiddle - React, Tailwind, and code Playground
HTML
<div contenteditable="true" id="selector" style="background-color: rgb(255,255,0);">
Test back<span style="background-color: rgb(255,0,0);">ground</span> color.
</div>
JavaScript
document.designMode = "on";
var selectionParent = function () {
var parent = null, sel;
if (window.getSelection) {
sel = window.getSelection()
if (sel.rangeCount) {
parent = sel.getRangeAt(0).commonAncestorContainer
if (parent.nodeType != 1) {
parent = parent.parentNode
}
}
} else if ( (sel = document.selection) && sel.type != "Control") {
parent = sel.createRange().parentElement()
}
return parent
}
$('div#selector').on('mouseup', function (){
alert($(selectionParent()).css('background-color'));
});