Home > Mobile >  Change image source through javascript does not seem to work. What am I doing wrong?
Change image source through javascript does not seem to work. What am I doing wrong?

Time:08-27

I'm trying to change the current logo of this website: https://moonstrategy.io/about/ from

https://moonstrategy.io/wp-content/uploads/2022/08/Moonstrategy_logo_vector_main_light_final.svg to https://moonstrategy.io/wp-content/uploads/2022/08/Moonstrategy_logo_vector_main_final.svg

I'm embedding this code in the webpage:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>document.getElementByClassName(".tgp-exclude small").src="https://moonstrategy.io/wp-content/uploads/2022/08/Moonstrategy_logo_vector_main_final.svg";</script>

I've also tried using different class names and while I'm pretty sure I tried all of them, I can either not seem to find the right one, or I am totally off with the code.

Help would be appreciated.

CodePudding user response:

  1. You included jquery, but you don't use jquery
  2. There is no such thing as getElementByClassName, there is getElementsByClassName
  3. getElementsByClassName returns NodeList and you want only one element, use querySelector instead.

CodePudding user response:

nothing wrong here but I think the document.getElementsByClassName() returns an array of elements with the specified class name So it should be something like

document.getElementsByClassName(".tgp-excludesmall")[0].src="https://moonstrategy.io/wp-content/uploads/2022/08/Moonstrategy_logo_vector_main_final.svg";
  • Related