JQuery The length of the element (s) inside each () function
Assuming I have the following HTML
<div class="news_item">
<div class="news_content">Some Content Here...</div>
<img src="something.jpg" />
</div>
I have the following jQuery code; which is supposed to count the number of IMG elements within a particular DIV and change the CSS DIV.news_content if there are no IMG elements.
$('div.news_item').each(function() {
if ($('img', this).length == 0) {
$('div.news_content', this).css('background-color', '#cccccc');
}
});
However, $ ('img', this) .length does not work inside every function.
+3
3 answers