Vishnu Vishnu - 6 months ago 18
jQuery Question

check if dynamically added images is loaded

I am creating simple html page where one image is dynamically added to div like below

$(".step3html").html('<img class="mobimg thumbnail" src="img/'+keys+'">');


I want to resize the container after image is loaded fully (it is absolute container) . So how to check if appended image is loaded and call some function where i will run resize function based on full container width.

I tried below code immediately after appending image,It doesn't seem to fire.

$(".mobimg").on('load', function() {
alert("das");
});


Here is fiddle to test https://jsfiddle.net/whhjtwpp/2/

Answer

This code below works.

$(".step3html").html('<img class="mobimg thumbnail" src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150">');

$(".mobimg").on('load', function() {
  alert("das");
});

Try this fiddle.

https://jsfiddle.net/pparas/whhjtwpp/

You might have a different issue. Have you checked the value of keys in your console?

EDIT

Here is a working fiddle.

https://jsfiddle.net/pparas/whhjtwpp/4/

Comments