I have an 'a' tag, that when clicked, removes the thumbnail from the gallery by calling a jquery click event. But if I dynamically add a thumbnail image and then remove it on the same page without refreshing, the click event doesn't get triggered. It triggers the modal window to pop up (as if I was clicking on the image). Then if I press the 'x' (to close) the modal window THEN triggers the jquery click event to remove the image. It works fine on non dynamically added thumbs or if the page has been refreshed.
$('a.close').click(function () {
var imageId = $(this).attr('id');
$.post(
'/ManageSpaces/RemoveImage',
{ id: imageId }
);
$(this).parents('li').remove();
});
<div id="row">
<ul id="sortable">
@foreach (var image in Model.Images.OrderBy(i => i.Ordering)) {
<li id="@image.YogaSpaceImageId" class="col-sm-6 col-md-4 imagethumbs" data-yogaspaceid="@Model.YogaSpaceId">
<div class="thumbnail">
<a id="@image.YogaSpaceImageId" class="close" href="#">×</a>
@{ var base64 = Convert.ToBase64String(image.ImageThumbnail); var thumbSrc = String.Format("data:image/gif;base64,{0}", base64); var base64Modal = Convert.ToBase64String(image.Image); var imgSrcModal = String.Format("data:image/gif;base64,{0}", base64Modal);
var imageId = "pop" + image.YogaSpaceImageId; var imagesourceId = "imagesource" + image.YogaSpaceImageId; }
<a class="image" id="@imageId" href="" data-toggle="modal" data-target="#myModal">
<img id="@imagesourceId" src="@thumbSrc" data-imagesrc="@imgSrcModal" alt="image not found" width="203" height="136" />
</a>
<div class="caption">
<h3>Thumbnail label</h3>
<p>...</p>
<p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a>
</p>
</div>
</div>
</li>
}
</ul>
<div class="modal fade" id="imageModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<a href="" data-toggle="modal" data-target="#myModal">
<img id="modalPreview" alt="image not found" width="355" height="355" />
</a>
</div>
</div>
</div>
</div>
this snippet of js adds the new thumbnail when a user selects a new image and then appends it to a ul list.
var listItem =
"<li id=\"" + responseText.YogaSpaceImageId + "\" class=\"col-sm-6 col-md-4 imagethumbs\" data-yogaspaceid=" + $("#HiddenYogaSpaceId").val() + ">" +
"<div class=\"thumbnail\">" +
"<a id=\"" + responseText.YogaSpaceImageId + "\" class=\"close\" href=\"#\">×</a>" +
"<a class=\"image\" id=\"" + imageId + "\" href=\"\" data-toggle=\"modal\" data-target=\"#myModal\">" +
"<img id=\"" + imagesourceId + "\" src=\"" + thumbSrc + "\" data-imagesrc=\"" + imgSrcModal + "\" alt=\"image not found\" width=\"203\" height=\"136\" />" +
"</a>" +
"<div class=\"caption\">" +
"<h3>Thumbnail label</h3>" +
"<p>...</p>" +
"<p><a href=\"#\" class=\"btn btn-primary\" role=\"button\">Button</a> <a href=\"#\" class=\"btn btn-default\" role=\"button\">Button</a></p>" +
"</div>" +
"</div>" +
"</li>";
var $li = $(listItem);
$('ul#sortable').append($li);