Let's say I have a text with mark, such as:
And I would like to disable selecting such that start or end of selection is not in the mark. Therefore, all of these selections should be possible:
On the other hand, these selections should be disabled:
So far, I tried only using some simple css,
mark {
-khtml-user-select: all;
-webkit-user-select: all;
-o-user-select: all;
user-select: all;
}
This is <mark>marked text</mark>. I want to disable selecting only part of <mark>marked text</mark>.
Is there any way to do this? Any answer would be appreciated!
I achieved what I wanted to do, so I share it here.
function checkSelection () {
var sel = window.getSelection();
var marks = document.getElementsByTagName('mark');
for(var i = 1; i < sel.rangeCount; i++) {
sel.removeRange(sel.getRangeAt(i));
}
var range = sel.getRangeAt(0);
var startnode = range.startContainer;
var endnode = range.endContainer;
for (var i = 0; i < marks.length; i++) {
if (marks[i].contains(startnode)) {
range.setStartBefore(startnode);
}
if (marks[i].contains(endnode)) {
range.setEndAfter(endnode);
}
}
}
document.addEventListener('mouseup', checkSelection)
document.addEventListener('touchend', checkSelection)
This is <mark>marked text</mark>. I want to disable selecting only part of <mark>marked text</mark>.