I have a question. I have a work this morning but i don’t know how to do it.
My work here (html):
<div class="demo">
<p>this is demo text</p>
</div>
Here is my JS:
var tempdata = $(".demo").text();
var replacedata = tempdata.replace("text","<span>1234</span>");
Look everything ok. But result is: this is demo <span>1234</span>. This isn’t my result I want. How to make in this string become a HTMLelement by using replace method?
When assigning the value back, use
.html()instead of.text()so it won’t encode it, like this:You can see a demo here
Also, you can pass a method to
.html()if you’re doing this to many elements, like this:Note this uses
.html()as the source as well, but makes no difference for the example code.You can see a demo of that here