JQuery .length of element(s) inside each() function -


assuming have following html

<div class="news_item">     <div class="news_content">some content here...</div>     <img src="something.jpg" /> </div> 

i have following jquery code; suppose count how many img elements there within particular div , change css of div.news_content if there 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 not seem work inside each function.

it sounds it's working, here alternative code filters out div's img children.

$('div.news_item').not(function(index){return $(this).find('img').length > 0;}).each(function(index, elem){      $(this).css('background-color', '#cccccc'); }); 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -