DasSaffe
DasSaffe

Reputation: 2198

jQuery: Replace every char on page with an image

Need your help, maybe just tunnel-vision right now.

I just want to replace every occurrence of a special character with an image using jQuery. Can't be that hard, can it?

I found several questions and I've been through them, but maybe I'm just missing something.

In that case, I'll try to replace every dot . with an image from an external resource (which is accessible) regardless of it's container. It doesn't make a difference if it is in a <p> or <div> or even just standing alone.

$("body").html().replace('/\./g', "<img src='my external image link'>");

Shouldn't that just do it?

Upvotes: 0

Views: 29

Answers (1)

Oliver Baumann
Oliver Baumann

Reputation: 2289

You need to re-assign the string you are building from the body's HTML.

Something like

let newHtml = $("body").html().replace(/\./g, "<img src='my external image link'>");
$('body').html(newHtml);

However, I'd be careful with replacing the '.'-character in the entire HTML, as it can be valid content of e.g. URLs in anchors. But I'm sure you thought about that already :)

Edit: Fixed RegEx-syntax. You should leave away the single-quotes around the RegEx.

Upvotes: 2

Related Questions