attr method gets the attribute value for only the first element in the matched set. declare empty variable for img data-src attribute.carouselImages() ) You are using jquery function ,so dont need for loop .Use each will help you more easily and set src with current element by (this). the src attribute is not replacing.("image1").prop("src", "images/img002.jpg") Working Demo. The reason is because the replace method actually returns the new string - not edits it in place. I am trying to replace the img source of a given source using jQuery. For example, when the image src is smith.gif, replace to johnson.gif. If williamEDIT: The images are retrieved from an XML to a random order, without class to each . This is what I tried: if ( (" img").attr(src, http jQuery provide 3 methods to get the content from a DOM element i.e. text(), html() and val().Example.