As in the attached image, my goal is to select a text, for instance "Among the Helvetii" and find which element among those of class nMarker was before the selection.
In other words, I’d like to select "Among the Helvetii" and find somehow the element of class nMarker, which has data-marker equal to 1.
Unfortunately I have not a single chance to use jQuery prevAll, prev, closest, etc. as the DOM is unknown, so I can’t count of parents, ancestors, etc. as the marker is placed after a number of words, following an algorithm (it is a requirement I can’t change).
What I achieved so far was to implement this cose, which works if the selection is in the same element where the marker is. But I’d like a solution working without knowing the DOM structure, as I said.
var sel = window.getSelection ? window.getSelection() : document.selection.createRange();
if(sel.getRangeAt){ // That’s for Firefox
var range = sel.getRangeAt(0);
var newNode = document.createElement("span");
newNode.setAttribute('class', 'justSelected');
range.surroundContents(newNode);
$(".justSelected").prevAll(".nMarker").data( "marker")
}
…marked text…
…marked text…
` also occur? – CBroe Oct 30 '20 at 07:57