I want to dynamically generate an input field based on the content of a paragraph containing some img tags (i.e. emoticons) and want to replace those images (the entire img tag) with an equivalend string (e.g. 🙂)
I tried the following function and it just does not work as expected. (Even when I alert the data and use it for replacement)
Moreover, as I have many images I thought there would be a better way to find and replace the entire tag in a string with jQuery. Any idea?
TXT = convertSmiley(TXT);
var convertSmiley = function(data) {
data = data.replace('<img src="/images/smiley/happy.png" style="margin-bottom:-4px;" height="16" width="16">',':)'); // which does not seem to work
return data;
}
you can use jQuery replaceWith()
http://jsfiddle.net/gPgr5/
Updated
for select by the image src, use
$('img[src$="happy.png"]')http://jsfiddle.net/gPgr5/1/