Replacing text with a link in jQuery

Posted by Eli on Stack Overflow See other posts from Stack Overflow or by Eli
Published on 2010-05-30T15:02:47Z Indexed on 2010/05/30 15:12 UTC
Read the original article Hit count: 209

Filed under:
|
|

I'm trying to replace a small part of text in a large HTML document with my own element which I create on the fly.

The text may be a huge bulk of text, html, images, what-ever, and what I want is to find the first (or all) the position of a certain string, and replace it with an element that I create using $('< span>').

Using simple text.replace('the string', $('< span>')); doesn't do the trick (I'm left with [object Object] and not the actual < span> that I want.

The reason I don't just inject direct HTML is because I want to retain all the binds that are related to the object I'm creating. and doing a replace with a custom ID, and then attaching binds to the ID after the HTML has been altered, seems a bit dirty.

Thanks for the help! :)

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about string