Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery image load callback

Dynamically adding an image to the page after load then user interaction and am having trouble firing a function after that image has completely loaded. I figured that using jQuery's .load() method would be fine.. but according my console and a bunch of documenting .log business it's never executed. See below.. thank you!

$('body')
    .append('<img id="ad" src="img/ad.jpg" alt="Advertising Network" />')
    .load(function(){
        console.log('ad load complete');
        $('#submit').click();
    });

UPDATE:

To clarify, this is after both document and window load.

like image 293
technopeasant Avatar asked May 28 '12 20:05

technopeasant


1 Answers

In your code .load() is related to the body because of chaining.

Change the code to:

var img = $('<img id="ad" src="img/ad.jpg" alt="Advertising Network" />');
img.load(function(){
    console.log('ad load complete');
    $('#submit').click();
});

$('body').append(img);

and it should work (http://jsfiddle.net/zerkms/f7epb/)

like image 64
zerkms Avatar answered Nov 13 '22 06:11

zerkms