1

I am working on a script and I need to change an image. The image has no class and I cannot set a class for it. How can I get the image by url and be able to edit it? Sort of like document.getElementById(); but with the image url. I have tried using getElementsByClass but that didn't work.

  • 2
    `querySelector()` – epascarello Jan 04 '21 at 16:33
  • Use [querySelector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) with an [attribute selector](https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Attribute_selectors). – Teemu Jan 04 '21 at 16:34

1 Answers1

0

You can select based on the src attribute.

const img = document.querySelector('img[src=' + url + ']');
Unmitigated
  • 76,500
  • 11
  • 62
  • 80