Credit to @Blender for this (posting as an answer as requested):
text = "mytext"
html = "<div><p><span></span></p></div>"
newHtml = $(html);
newHtml.find('*:last').text(text);
console.log(newHtml.get(0)); // <div><p><span>mytext</span></p></div>
So construct a jQuery object from the html
string, then find the :last
descendent (in this example, the span
), and set the text
.