Search code examples
javascripthtmldomtampermonkey

Selecting an image element by using the source attribute to find it


I'm working on a script for a website, and I need to change the icon of one of its images. The problem is the image on the site only has a tag like this:

<img src="/pic1.png" alt="clear" >

I have tried selecting the image by using:

document.querySelector("img[src='pic1.png']");

What I have tried looks like this:

var y = document.querySelector("img[src='/pic1.png]'");
y.innerHTML = "<img src='pic2.png' alt='clear'>";

I also tried

var y = document.querySelector("img[src='/pic1.png]'");
y.src = "/pic2.png";

Is there anyway I can find the image using the src attribute, assign it to a variable, then replace the src?

Your help is appreciated :)


Solution

  • Your last example should work

    var y = document.querySelector("img[src='/pic1.png']");
    y.src = "/pic2.png";
    y.load()