I want to do something similar to splitText(), but with length, so if I have a Text node that contains the following string for example:
Hello please get *me* as a text node
Then only get *me* as a new Text node.
splitText() just splits into two so that's not working
You can use the normal String.split method, which is more straight forward if you know you want to split on the asterisk.
const textNode = document.body.firstChild
const text = textNode.data.split("*")[1]
const newTextNode = document.createTextNode(text)
document.querySelector("strong").append(newTextNode)
Hello please get *me* as a text node
<br> new textNode: <strong><strong>
You can also use substring.
const textNode = document.body.firstChild
const text = textNode.data.substring(22, 26)
const newTextNode = document.createTextNode(text)
document.querySelector("strong").append(newTextNode)
Hello please get *me* as a text node
<br> new textNode: <strong><strong>