I am trying to bind a load handler to a dynamically created object (backbone view in my production code). I tried to use the approach outlined in In jQuery, how to attach events to dynamic html elements?, and it works for a click handler but not a load handler. Does anyone know how to solve this?
This works (with click handler)
$(document).ready(function() {
$("body").on("click", "img", function(){
console.log("foo");
});
create();
});
function create(){
$img = $("<img />").attr("src", "http://www.pureweber.com/wp-content/uploads/2011/04/jquery_icon.png");
$("body").append($img);
}
But this doesn't (with load handler)
$(document).ready(function() {
$("body").on("load", "img", function(){
console.log("foo");
});
create();
});
function create(){
$img = $("<img />").attr("src", "http://www.pureweber.com/wp-content/uploads/2011/04/jquery_icon.png");
$("body").append($img);
}