Skip to content

How do replace some of the text in an attribute in all classes? (pure js)

I have a working example in jquery, but there are compatibility issues, so I want to rewrite this in pure javascript. How do I do this correctly?

$(setInterval(
    function(){
        $('.swiper-zoom-container > img').each(function(){
    var srcset = $(this).attr('srcset');;
    $(this).attr('srcset', srcset.replace('s=1000x700','s=4000x4000'));
  });
    }, 300
));

My attempt was like that, but it’s not a workable option:

var elements = document.querySelectorAll('div.swiper-zoom-container > img');

setInterval(function() {
  elements.srcset = elements.srcset.replace('s=1000x700','s=4000x4000');
}, 300);

Answer

var elements = document.querySelectorAll('div.swiper-zoom-container > img');

setInterval(function() {
  elements.forEach(el => el.srcset = el.srcset.replace('s=1000x700','s=4000x4000'));
}, 300);