0

I am trying to replace the "www" in src with "refresh" for a collection of images with class "cover".

let testing = document.querySelectorAll(".cover");

for (index = 0; index < testing.length; index++) {
    var x = document.querySelectorAll(".cover");
    x.src = x.src.replace("www", "refresh");
} 
Dpuiatti
  • 95
  • 1
  • 9

1 Answers1

3

Try

let testing = document.querySelectorAll(".cover");

testing.forEach(function(el) {
  el.src = el.src.replace("www", "refresh");
});
Dominik Matis
  • 2,086
  • 10
  • 15
  • 1
    Thank you, this works! So you selected each item in the array by using "forEach"? – Dpuiatti Aug 09 '19 at 19:45
  • Exactly, what you did in loop is that you get all the `NodeList` array again and again, you though it was single element, but it was array of all matched elements, exactly what was in `testing`. If this is correct, please mark it as correct answer :) Glad to help and have a nice day :) – Dominik Matis Aug 09 '19 at 19:47