Changing images in javascript

I have this code to change images in javascript:

document.getElementById(img).src = "";

But I need to use the internal (base64) media passage instead of an URL. How can I do it?

1 Like

You need to convert your image to base 64 string.

You can use any online converter like:

Here is a codepen with the code working:

If you need to convert it “on fly”, thats another problem : )

If you’ve already embedded the image as an actual media passage, via a compiler or manually, then the passage’s source is all you need, being the Base-64 encoded form of the image.

Try the following:

document.getElementById(img).src = Story.get("image passage name").text.trim();

That was it! Thanks. I knew I could count on you! I imagined the first part and struggled trying to figure out what the second part was, I tried “processtext” and it didn’t work. But you always help me, I’m very grateful!

Thanks, I use Twine Enhancer to create Media Passages “on the fly”, and my question was about how to change an image in javascript. But TheMadExile has solved that. Thanks anyway.

1 Like