Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using jquery, how can i select all images inside of a div to change its source

Tags:

jquery

image

I have the following div and I know the selector Id of the DIV.

<div class="event">
  <img src="/Content/Images/Icons/calendar1.png">
  <img src="/Content/Images/Icons/calendar2.png">
  <img src="/Content/Images/Icons/calendar3.png">
  <img src="/Content/Images/Icons/calendar4.png">
  <img src="/Content/Images/Icons/calendar5.png">
  <img src="/Content/Images/Icons/calendar6.png">
</div>

I need something to find all images selector inside the div that i have so i can go change the source of the each image to a new image.

like image 464
BenyaminRmb Avatar asked Oct 23 '25 03:10

BenyaminRmb


1 Answers

Use this code to retrieve the image URL:

$('.event img').each(function(){
  alert($(this).attr('src'));
});

if you want to change the image URL then use this:

var inc = 1;
$('.event img').each(function(){
  $(this).attr('src','path of image/imagename'+inc+'.imageextension');
  inc++;
});
like image 74
Code Lღver Avatar answered Oct 25 '25 06:10

Code Lღver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!