I want to select a part of a text with JavaScript. I want to specify the range based on length not using nodes. For instance: I have text "AB DDSD" and I want to select from 0-3. I cannot wrap it with an element like span
because the Arabic letters do not concatenate.
The following example selects all text inside foo
class but how to specify a range for it?
var selection = window.getSelection();
var txt = document.getElementsByClassName("foo");
var range = document.createRange();
range.selectNodeContents(txt);
selection.removeAllRanges();
selection.addRange(range);
I am open to using other methods instead of select
. My goal is to color certain characters in p
tag but cannot wrap it using any element. If I wrap only one character of سس
then it turns into س س
.