I'm still new to Javascript and I'm using the web for research while trying stuff out. I can however not find any reference on how to create an element at a specific index within the text content of an element.
I'm trying to create a highlighting function via JS, and this is what I've ended with so far
let indexes = []
for (i = 0; i < document.body.innerText.length; i++) {
if (document.body.innerText[i] == "`") {
if(document.body.innerText[i - 1] != "\\") {
indexes.push(i)
}
}
}
if (indexes.length % 2 != 0) {
indexes.splice(indexes.length - 1)
}
console.log(indexes)
`body`, \`body`
The idea is to create a wrapping <span>
element from the range of the first index to the second, so on..., I'm baisically looking for a JS functin that would allow me to create an element at that specific index.
E.g.
`body` --> <span>body</span> | `Lorem ipsum, dolor` --> <span>Lorem ipsum, dolor</span>
It's probably just a quick hint and not someting major, but thanks anyways!