0

I would replace Pictures on mouseover.
I Found a nice script here, but the problem is all images on the page get replaced.

This way i would filter the images while selecting.
I added each picture the ending "_rep".

The code im using:

$("img")
  .mouseover(function() {
    var src = $(this).attr("src").match(/[^\.]+/) + "_akt.png";
    $(this).attr("src", src);
  })
  .mouseout(function() {
    var src = $(this).attr("src").replace("_akt.png", ".png");
    $(this).attr("src", src);
  });

Search for something like that:

$("img like %_rep%")
//code mouseover, mouseout
Community
  • 1
  • 1

2 Answers2

2

If I understand correctly, you want to replace images which end of names are _rep ?

In that case you can do that :

$('img[src$="_rep.png"]')
KCarnaille
  • 1,027
  • 9
  • 18
1

use this

$("img[src$='_rep.png']")

[src] means all tags with an src attribute. and [src$='_rep.png'] means all tags with an src attribute ending with '_rep.png'

and take a look here https://api.jquery.com/attribute-ends-with-selector/ I recommend you look through all different kinds of JQuery selectors in the provided link.

azerafati
  • 18,215
  • 7
  • 67
  • 72