Please Guys, I am new to Jquery. I have been searching for a suitable help since last week but I can\'t find any. I want to replace a text with a smiley image using jquery but I
You can use following code:
var a = [[':happy;','<img src="happy_face.gif" />'],[':sick;',"<img src='sick.gif' />"]];
a.forEach(function(item) {
$('p').each(function() {
var text = $(this).html();
$(this).html(text.replace(item[0], item[1]));
});
});
Working example here: Smiley codes replace
It is actually quite simple.
You only need one line to find and replace string instances in the HTML:
$("body").html($("body").html().replace(/:happy;/g,'<div class="happy-face"></div>'));
This sets $("body").html()
to the $("body").html()
with :happy;
replaced with <div class="happy-face"></div>
.
/:happy;/g
is RegEx to find the string :happy;
globally (g
)
https://jsfiddle.net/heowqt1u/2/