Levent Çorapsız Levent Çorapsız - 1 year ago 78
jQuery Question

JQuery .on() event handler usage for dynamically loaded image

My pages are generating dynamically by ajax response.

Each response has an

element with specified id. I need them to fade in when loaded.

works fine when page is loaded for first time. But not working in next response for sure.


So i need to use
event handler. But doesn't work.


Note: This is not a cache issue. Img src also has a random query string.

Answer Source

onload event doesn't bubble, so you cannot delegate it. But, if you don't need to support IE8<, you can capture event instead which will work for any dynamic img:

        var elm = event.target;
        if( elm.id === 'my_img'){ // or any other filtering condition
            // do some stuff
    true // Capture event


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download