Hi everyone, I have a function that works as follows:
define(function () {
'use strict';
var sel, range, span;
return function () {
span = document.createElement("span");
span.className = 'highlight';
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount) {
range = sel.getRangeAt(0).cloneRange();
range.surroundContents(span);
sel.removeAllRanges();
sel.addRange(range);
}
}
};
});
This function basically wraps my selection with a span. Now, I am looking for a way to reverse this action and remove the span from the selected text. For instance
Let's consider a div:
<div>some text in here</div>
If I were to apply the above function on the word "here" within the div:
<div>some text in <span class="highlight">here</span></div>
I want a method that will eliminate the span element from the highlighted word when it is selected again.