Search code examples
jqueryimagesrc

Changing part of an image SRC with jQuery?


I came across a problem today, and I'm wondering if I can fix it. Is there a way to get each of the element (ol.notes .avatar - there are more than one ol.notes .avatar) and replace part of the image SRC with them? Each image ends with _16.png, but I need jQuery to replace that _16.png with _48.png. Is there a way to do this?

What I have now (not working)

    $('ol.notes .avatar').each(function(){
    this.src = this.src.replace("_16","_40");
});

Solution

  • $('ol.notes .avatar').attr('src', function(i, src) {
        return src.replace( '_16.png', '_48.png' );
    });