Using jQuery, how to modify text outside of tags?

Posted by Jacob on Stack Overflow See other posts from Stack Overflow or by Jacob
Published on 2010-04-30T04:02:01Z Indexed on 2010/04/30 4:07 UTC
Read the original article Hit count: 221

Filed under:
|
|

Given a string of text that is both adjacent to a span and inside of a div, what are some methods to modify just that text, leaving the surrounding HTML intact? For example:

<div id="my-div">modify this text<span id="my-span"></span></div>

I have tried things like

$('#my-div').html(function(i, elem){blah;});

but this seems to cause the span to be deleted and a new span to be added, because some styling is lost.

I realize that it would be best to wrap the text string in its own HTML tags before applying client-side code, but that is out of my control.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about html