Skip to content Skip to sidebar Skip to footer

Replace Image File Name By Jquery

I have find related answer but not same like this topic, so i asked this question. My question is, i'm trying to replace image file name default.jpg to hqdefault.jpg from all imag

Solution 1:

try this

$('#selector img').attr('src',function(i,e){
    return $(this).attr('src').replace("default.jpg","hqdefault.jpg");
});

Solution 2:

You can also trythis:

$('#selector img').on({
        'click': function(){
          var src1 = $(this).attr("src"); 
          var path = src1.substring(0,src1.lastIndexOf('/'));
          var new_source=path+'/'+'hqdefault.jpg';
            $(this).attr('src',new_source);
        }
    });

Solution 3:

You should use each function to iterate through all img. Following code snippet may help you.

$('#selector img').each(function(){
    var src = $(this).attr('src').replace("default.jpg","hqdefault.jpg");
    $(this).attr('src', src);
});  

Update: If you are using LazyLoad XT plugin then do the same in it's onload event like following.

$.lazyLoadXT.onload=function(){
      var src = $(this).attr('src').replace("default.jpg","hqdefault.jpg");
      $(this).attr('src', src);
 }

Post a Comment for "Replace Image File Name By Jquery"