# JavaScript to replace text string with image?

**URL:** <https://forums.ankiweb.net/t/javascript-to-replace-text-string-with-image/33966>\
**Category:** Card Design\
**Created:** [September 5, 2023, 1:11pm UTC](https://forums.ankiweb.net/t/javascript-to-replace-text-string-with-image/33966 "2023-09-05T13:11:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlmmr](https://avatars.discourse-cdn.com/v4/letter/m/df705f/32.png) [@mlmmr](https://forums.ankiweb.net/u/mlmmr)\
**Post date:** [September 5, 2023, 1:11pm UTC](https://forums.ankiweb.net/t/javascript-to-replace-text-string-with-image/33966/1 "2023-09-05T13:11:11Z")

</div>

Hi!

Is it possible to add a script to the card template that replaces every instance of a specific text string with a matching image previously saved to the Anki collection.media folder?

Example: every instance of the text “one” in these cards are replaced with the image `_one.png` from the media folder when reviewing.

---

<div class="post-metadata">

**Author:** ![mlmmr](https://avatars.discourse-cdn.com/v4/letter/m/df705f/32.png) [@mlmmr](https://forums.ankiweb.net/u/mlmmr)\
**Post date:** [September 10, 2023, 11:05am UTC](https://forums.ankiweb.net/t/javascript-to-replace-text-string-with-image/33966/2 "2023-09-10T11:05:57Z")

</div>

Could something like this be made to work?

```auto
<div id=front />
<script>
var str = document.getElementById("Front").innerHTML;
var res = str.replace(/one/g, "<img src='_one.png'>");
document.getElementById("Front").innerHTML = res;
</script>

```

---

<div class="post-metadata">

**Author:** ![kelciour](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/kelciour/32/14770_2.png) [@kelciour](https://forums.ankiweb.net/u/kelciour)\
**Post date:** [September 10, 2023, 11:33am UTC](https://forums.ankiweb.net/t/javascript-to-replace-text-string-with-image/33966/3 "2023-09-10T11:33:25Z")

</div>

```auto
<div id=front>{{Front}}</div>

<script>
var str = document.getElementById("front").innerHTML;
var res = str.replace(/one/g, "<img src='_one.png'>");
document.getElementById("front").innerHTML = res;
</script>

```

---

<div class="post-metadata">

**Author:** ![mlmmr](https://avatars.discourse-cdn.com/v4/letter/m/df705f/32.png) [@mlmmr](https://forums.ankiweb.net/u/mlmmr)\
**Post date:** [September 10, 2023, 12:57pm UTC](https://forums.ankiweb.net/t/javascript-to-replace-text-string-with-image/33966/4 "2023-09-10T12:57:02Z")

</div>

Thank you 🙏

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [October 10, 2023, 12:57pm UTC](https://forums.ankiweb.net/t/javascript-to-replace-text-string-with-image/33966/5 "2023-10-10T12:57:18Z")

</div>

Closed on October 10, 2023, 12:57pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
