Replace text with image HTML | Example Using JavaScript

Use regular expression and innerHTML to Replace text with an image in HTML. A regular expression is based on the text want to replace.

Example how to replace the text with image html

HTML example code of Replace Text with Image using JavaScript (getElementsByClass or getElementsByID ).


  • Use id for the text element
  • Function for the whole thing immediately execute
  • Use getElementsByClassName or idName
  • Loop through them
  • Operate on the innerHTML of each element.


replace text with image html JavaScript

Note: The above script tag will need to be placed after all of the elements you want to process in the HTML file.

Do comment if you have any better examples or have suggestion or doubts on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.