I have the following html:
<div id="d1"></div>This is a text that <div id="d2"></div>needs to be manipulated
and two phrases from the text that indicates a start and end position, say in this example:
start = "a text"
end = "needs"
How can I use javascript to add a span element with a start tag just before the start phrase, and an end tag just after the end phrase, like this?
<div id="d1">/div>This is <span id="phrase_1">a text that <div id="d2"></div>needs</span> to be manipulated
3
Answers
use this
It would be interesting to know in what kind of parent element your initial html is included in. Because, if you can target that element.
if you can get the parent element, then you could do:
Clean the tags first, you have extra
/div>
just after the<div id="d1">
tag.Its simple, just use
String.prototype.replaceAll()
function as mentioned in here.