I’m have a click function which adds an image to a div with a class of “selectedimage”. I’m trying to create another click function which references “selectedimage”, however, it will not initiate. Is this because it didn’t exist when the DOM was first loaded? Thanks.
$(".articlethumb").bind('click', function() {
var currentid = $(this).attr("id");
var medium = $(this).find(".thumb-medium").text();
var mediumimage = "<img id='" + currentid + "' class='selectedimage' src='" + medium + "' />";
$("#mainimage").html(mediumimage);
});
$(".selectedimage").bind('click', function() {
alert("test");
});
Use .live.
As you suspected it has to do with the element not being in the dom when the event is bound, and live fixes this.