Remove div if all img src is null inside div
Html code
<div class="add_pdt_img_nc">
<h5>Additional Images</h5>
<img border="0" src="" alt="">
<img border="0" src="" alt="">
<img border="0" src="" alt="">
<img border="0" src="" alt="">
</div>
I want to remove a div, has class add_pdt_img_nc if all src image is null
I've already tried
$(".add_pdt_img_nc img").each(function() {
if($(this).attr("src") == "") {
$(".add_pdt_img_nc").remove();
}
});
but it removes the div if first img src is null and doesn't check if other img src has value
+3
thecodedeveloper.com
source
to share
3 answers
var all_null = true;
$(".add_pdt_img_nc img").each(function() {
if($(this).attr("src")!= "") {
all_null = false;
return false; // break
}
});
if(all_null) {
$(".add_pdt_img_nc").remove();
}
+6
Peter
source
to share
Try the following:
$(".add_pdt_img_nc").each(function() {
var $parent = $(this);
if ($('img[src!=""]', this).length == 0) {
$parent.remove();
}
});
Sample script
This checks for the existence of elements img
inside .add_pdt_img_nc
that have the attribute src
. If not, it deletes div
.
+3
Rory McCrossan
source
to share
Use this:
$("div").each(function(){
if ($('img[src!=""]', this).length == 0 || !$('img').attr("src")) {
$(this).remove();
}
});
+3
Praveen kumar
source
to share