Skip to content
Advertisement

JS Change HTML img src based on specific text found in h1 tag

I’ve posted a similar question regarding switching HTML text snippets (see JS Switch HTML content based on specific text included in h1 tag), for which the replies were hugely helpful. From these replies, I used the following code to change the HTML text snippet:

JavaScript
JavaScript

I now wish to modify the code to change the img src depending on some text (a different string of text from that required in my previous post) included in the h1 tag.

After lots of trial and error I can’t figure out how to modify this code to change the image src on the same page. The closest I’ve got so far, but using the “if/else” method, is:

JavaScript
JavaScript

However, I still cannot get this to work. I’m really hoping someone can help! Many thanks in advance!

Advertisement

Answer

For what I see you have 2 errors in your code.

1 – remove .imageNode.

2 – you need to convert text to lower-case as the object key you are trying to get is in lower-case, use .toLowerCase()

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement