jquery loop through images and replace src

each, jquery

Solution

Make sure all the DOM elements are ready before changing your image source by wrapping your code inside `$(function() { })`

$(function() {
    jQuery('.element img').each(function () {
        jQuery(this).attr('src', jQuery(this).attr('src').replace("old/directory", "new/directory"));
    });
});

Problem

I need to loop through all images in DOM and replace a specific source. Don't know why this isn't working. UPDATE I need to search and replace in the src of each image. If the source contains "old/directory" ONLY then replace to "new/directory". ``` jQuery('.element img').each(function () { jQuery(this).attr('src', jQuery(this).attr('src').replace("old/directory", "new/directory")); }); ```

Original source