jQuery way to replace just a text node with a mix of HTML and text

Posted by hippietrail on Stack Overflow See other posts from Stack Overflow or by hippietrail
Published on 2012-10-12T03:17:13Z Indexed on 2012/10/12 3:37 UTC
Read the original article Hit count: 178

In my web browser userscript project I need to replace just one text node without affecting the other HTML elements under the same parent node as the text. And I need to replace it with more than one node:

<div id="target">
    some text<img src="/image.png">
</div>

Needs to become:

<div id="target">
    <a href="#">mixed</a> text <a href="#">and</a> HTML<img src="/image.png">
</div>

I know jQuery doesn't have a whole lot of support for text nodes. I know I could use direct DOM calls instead of jQuery. And I know I could just do something like $('#target').html(my new stuff + stuff I don't want to change).

What I'd like to ask the experts here is, Is there a most idiomatic jQuery way to do this?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about dom-manipulation