0

I have a simple jquery script that changes the url path of the images. The only problem is the doesn't apply after I click the load more button. So I'm trying to do a workaround where it calls the script again after clicking the button.

<script type='text/javascript'>
$(document).ready(function ReplaceImage() {
    $(".galleryItem img").each(function() {
        $(this).attr("src", function(a, b) {
            return b.replace("s72-c", "s300")
        })
    })
});
</script>

HTML

<a href="javascript:;" onclick="ReplaceImage();">Load More</a>
1
  • 3
    Your ReplaceImages is anonymous named function inside your .ready().. move it outside the .ready, and then do $(document).ready(ReplaceImage) Commented Oct 7, 2016 at 0:07

1 Answer 1

1

While Keith's answer will get you what you are looking for, I really can't recommend that approach. You are much better off with something like this.

<script type="text/javascript">
$(function() {

    var replaceImage = function() {
        $('.galleryItem img').each(function() {
            $(this).attr('src', function(index, value) {
                return value.replace('s72-c', 's300');
            });
        });
    };
    replaceImage();

    $('.js-replace-image').on('click', replaceImage);

});
</script>

Using this html

<button class="js-replace-image">Load More</button>

By taking this approach, you do not expose any global variables onto the window object, which can be a point of issue if you work with other libraries (or developers) that don't manage their globals well.

Also, by moving to a class name and binding an event handler to the DOM node via JavaScript, you future proof yourself much more. Also allows yourself to easily add this functionality to more buttons very easily but just adding a class to it.

I updated the anchor tag to a button because of the semantics of what you need to do - it doesn't link out anywhere, it's just dynamic functionality on the page. This is what buttons are best served for.

I'd also recommend putting this in the footer of your site, because then, depending on your situation, you will already have the images updated properly without having to click the button. The only need for the button would be if you are dynamically inserting more images on the page after load, or if this script was in the head of your document (meaning jQuery couldn't know about the images yet).

I hope this helps, reach out if you have questions.

Sign up to request clarification or add additional context in comments.

1 Comment

If your JavaScript is being included via an external file you can use the defer attribute in the script tag to ensure the script is not run until the DOM is fully loaded. Although you are using jQuery's ready() which is pretty solid about not executing until everything is loaded so you might be okay.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.