1

I would like to change the image source of each image when it is hovered. I will have the original url that exists before the hover (represented by yes.png), and the new url that exists after the hover (represented by yes2.png). The whole point is that the new url is the same as the original except a 2 is added to it on hover. And I would like this done in javascript. I'm new at javascript, but how do I add onto this?

The whole thing that makes this different is that the source of the image is different for each image, yet it works for every image.

var url = getElementsByClassName ('card-img-top').src
var newImg = url+2
lexi b
  • 65
  • 1
  • 1
  • 6

3 Answers3

1

You can do this relatively easily. You can find where .png is in the string and insert a 2 before it on mouseenter and remove the 2 on mouseout.

I'm not sure if you just want the image that is being hovered over to change or all images so I'll include an example of how to do both. First and probably what you are looking for I'll change only the image that is being hovered over:

const imgs = document.getElementsByTagName('img');

[...imgs].forEach(img => {
  img.addEventListener('mouseenter', () => {
    const newSrc = img.src.slice(0, img.src.indexOf('.png')) + 2 + img.src.slice(img.src.indexOf('.png'));
    console.log(newSrc);
    img.src = newSrc;
  })
  img.addEventListener('mouseout', () => {
    const newSrc = img.src.slice(0, img.src.indexOf('.png') - 1) + img.src.slice(img.src.indexOf('.png'));
    console.log(newSrc);
    img.src = newSrc;
  })
});
img {
  height: 100px;
  width: 100px;
}
<img src="pic.png">
<img src="img.png">

Second I'll change the src of all img tags whenever one is hovered over:

const imgs = document.getElementsByTagName('img');

[...imgs].forEach(img => {
  img.addEventListener('mouseenter', () => {
    [...imgs].forEach(img => {
      const newSrc = img.src.slice(0, img.src.indexOf('.png')) + 2 + img.src.slice(img.src.indexOf('.png'));
      console.log(newSrc);
      img.src = newSrc;
    })
  })
  img.addEventListener('mouseout', () => {
    [...imgs].forEach(img => {
      const newSrc = img.src.slice(0, img.src.indexOf('.png') - 1) + img.src.slice(img.src.indexOf('.png'));
      console.log(newSrc);
      img.src = newSrc;
    })
  })
});
img {
  height: 100px;
  width: 100px;
}
<img src="pic.png">
<img src="img.png">
Cory Kleiser
  • 1,969
  • 2
  • 13
  • 26
0

Add listeners for the mouseover and mouseout events.

getElementsByClassName returns a collection, you have to loop over it to add handlers for all the elements.

const cards = getElementsByClassName('card-img-top');
Array.from(cards).forEach(card => {
    card.addEventListener("mouseover", function() {
        let url = this.src;
        let newImg = url + "2";
        this.src = newImg;
    });
    card.addEventListener("mouseout", function() {
        let url = this.src;
        let newImg = url.substr(0, -1);
        this.src = newImg;
    });
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Not sure why you are changing the source. However, could you try using a sprite as the background image and change background position with :hover css pseudo-selector

This another SO question could help you: Creating CSS Sprite :hover Roll Over Image Links

  • Oh! I'm changing the source because the images are previews of a product. Typically there is a second image of the product when you hover on the image. Thanks. – lexi b Jun 08 '18 at 03:02