Detect an image exchange

Asked

Viewed 87 times

0

Guys I have an img tag that has a button that loads several images for the person to choose, so he chooses the src of this tag is changed. I need to detect when this src change happens and show an alert. I tried with the onchange but it did not fire, with the load worked but it also fired on the page loading because as soon as the page loads there appears a standard image.

$(".imagem").on("change", function(e){alert("alterou")});
  • 2

    It is not better to do that where the image is changed?

  • 1

    Whether the user chooses when to change the src why don’t you use that event/moment? you want to know load of that image when the src change only? Then you can join that Handler Event you have on top > change to src > remove Event Handler when the load be called

  • @Sergio I couldn’t understand your explanation.

  • @Lucas I really need to check the change of SRC has no way to be when the image is changed.

  • What’s the code for when "the person chooses"?

  • @Sergio is in a modal with an image gallery. When he chooses the image he triggers this code: `$('#image'+column, window.parent.Document). attr('src', file);

  • 1

    @Joaonivaldo then you can detect there when the image changes and show the right alert?

Show 2 more comments

1 answer

2


You need to register the event with the load after the page has been fully loaded:

$(window).load(function() {
    $('.image').on('load', function() {
        alert('Imagem alterada');
    });
});

See working on Jsfiddle

  • Thank you very much gave certinho @luciorubeens.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.