("*").each(function () {
if ($(this).children().length == 0) {
$(this).text($(this).text().replace('basketball','test'));
}
});
i'm only able to change the text to another string of text, but how can I pass an image?
("*").each(function () {
if ($(this).children().length == 0) {
var newHTML = $(this).html().replace('basketball','<img src = "image.jpg" />');
$(this).html(newHTML);
}
});
EDIT
My mistake. I thought you wanted to replace the entire element. Check out my updated answer.
You need to modify the html() rather than the text().
You can simplify your code to
$("*").filter(function() { return !$(this).children().length; })
.html(function(index, old) { return old.replace('basketball', '<img ... />'); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With