C
C
Collaboration2022-02-27 16:14:51
JavaScript
Collaboration, 2022-02-27 16:14:51

How to add src to all images?

Hey! I create a form for uploading pictures, everything is fine, but the pictures after the replacement have the same address, respectively, until you clear the cache - you see the old picture. Found a solution:

$(function() {
var a = $('.reload-img img').attr('src');
$(".reload-img img").attr("src", (a) + "?" + Math.random());
});


Everything is almost good, except that the changed src of the first image is distributed to all images that are in .reload-img. How to make it so that this random text is added to the current src of each image ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wakenbyWork, 2022-02-27
@EgorShorin

$('.reload-img img').each(function () {
  const src = $(this).attr('src')
  $(this).attr('src', `${src}?${Math.random()}`)
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question