The problem is that your id
value isn't valid for a simple CSS ID selector (it's a valid HTML id
, but HTML and CSS have different rules).
While you could use $('[id="gc_photo_' + fullpath + '"]').remove();
(note there's no#
at the beginning) to remove it, you don't need to. You don't need the id
at all, just remove the containing .row.gc_photo
div:
function remove_image(img) {
if (confirm(/*...*/)) {
img.closest(".row.gc_photo").remove();
}
}
Live Example:
function remove_image(img) {
if (confirm("Delete the image?")) {
img.closest(".row.gc_photo").remove();
}
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet"/>
<div class="row gc_photo" id="gc_photo_546_some_file_name.jpg" data-file="some_file_name.jpg" data-image="some product name" style=" border-bottom:1px solid #ddd; padding-bottom:20px; margin-bottom:20px;">
<div class="col-md-4">
<div class="form-group">
<input name="images_alt[546][alt]" value="the alt text" class="form-control" placeholder="some placeholder" />
</div>
</div>
<div class="col-md-4">
<a onclick="return remove_image($(this));" rel="546_some_file_name.jpg" class="btn btn-danger pull-right"><i class="icon-times "></i></a>
</div>
</div>
<div class="row gc_photo" id="gc_photo_889_another_file.png" data-file="another_file.png" data-image="another image" style=" border-bottom:1px solid #ddd; padding-bottom:20px; margin-bottom:20px;">
<div class="col-md-4">
<div class="form-group">
<input name="images_alt[889][alt]" value="another alt" class="form-control" placeholder="another placeholder" />
</div>
</div>
<div class="col-md-4">
<a onclick="return remove_image($(this));" rel="889_another_file.png" class="btn btn-danger pull-right"><i class="icon-times "></i></a>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Side note: You could remove the onclick=return remove_image($(this));"
attribute in favor of hooking things up dynamically. All of these are in some kind of container (at worst document.body
, but probably there's a closer, stable container). You can hook them up with event delegation (so you don't have to worry about it when you add more at runtime). Give the buttons a class:
<a rel="546_some_file_name.jpg" class="delete-image btn btn-danger pull-right"><i class="icon-times "></i></a>
...then:
$("selector-for-the-container").on("click", ".delete-image", function() {
if (confirm(/*...*/)) {
$(this).closest(".row.gc_photo").remove();
}
return false;
});
Live Example:
$(".some-container").on("click", ".delete-image", function() {
if (confirm("Delete the image?")) {
$(this).closest(".row.gc_photo").remove();
}
return false;
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet" />
<div class="some-container">
<div class="row gc_photo" id="gc_photo_546_some_file_name.jpg" data-file="some_file_name.jpg" data-image="some product name" style=" border-bottom:1px solid #ddd; padding-bottom:20px; margin-bottom:20px;">
<div class="col-md-4">
<div class="form-group">
<input name="images_alt[546][alt]" value="the alt text" class="form-control" placeholder="some placeholder" />
</div>
</div>
<div class="col-md-4">
<a rel="546_some_file_name.jpg" class="delete-image btn btn-danger pull-right"><i class="icon-times "></i></a>
</div>
</div>
<div class="row gc_photo" id="gc_photo_889_another_file.png" data-file="another_file.png" data-image="another image" style=" border-bottom:1px solid #ddd; padding-bottom:20px; margin-bottom:20px;">
<div class="col-md-4">
<div class="form-group">
<input name="images_alt[889][alt]" value="another alt" class="form-control" placeholder="another placeholder" />
</div>
</div>
<div class="col-md-4">
<a rel="889_another_file.png" class="delete-image btn btn-danger pull-right"><i class="icon-times "></i></a>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>