Have whats probably a simple problem with using parent in if and each statements
I have some html that looks like:
<li><span class="image"><img src="img1.jpg"></span></li>
<li><span class="image"><img src="img2.jpg"></span></li>
<li><span class="image"><img src=""></span></li>
<li><span class="image"><img src=""></span></li>
With my JQuery I have an if statement within an each statement as follows:
$(".image").each(function() {
var $imgsrc = $(this).find("img").attr("src").length;
if($imgsrc < 1) {
$('.image').parent().addClass('nodisplay');
}
});
I want the to add the class ‘nodisplay’ be added to the <li>‘s whose images have no src.
Problem is the code above affects all <li>‘s. If I replace $('.image') with $('this') it no longer works at all.
I have also tried closest('li') instead of parent() to no avail.
Any advice would be greatly appreciated! Thanks
Try this:
Live demo