If I have the following HTML:
<div class="content">
Vivamus <span>luctus</span> urna sed urna ultricies ac tempor dui sagittis.
</div>
And I run an event on mouseup
that sees the ranges of the selected text:
$(".content").on("mouseup", function () {
var start = window.getSelection().baseOffset;
var end = window.getSelection().focusOffset;
if (start < end) {
var start = window.getSelection().baseOffset;
var end = window.getSelection().focusOffset;
} else {
var start = window.getSelection().focusOffset;
var end = window.getSelection().baseOffset;
}
console.log(window.getSelection());
console.log(start + ", " + end);
});
And I select the word Vivamus
from the content, it will log 1, 8
, as that is the range of the selection.
If, however, I select the word urna
, it will log 15, 20
, but won't take into account the <span>
elements of the HTML.
Is there anyway for focusOffset
and baseOffset
to also count for HTML tags, instead of just the text?
<span>
elements surround the text, so that I can give the effect that they're highlighted. When you selected text that is already selected and text that's not, I want them to merge into one highlight. I don't like the plugins that are out there for this, as they're way too bloated, especially Rangy (I tried to use it). – Dulia