mercredi 22 juin 2016

Loop Through Divs and Check If Child Div Has Content

I'm looking to loop through divs, check if each has content within it's specific child div, if so add a class to the div.

  $('.img-banner').each(function( index, element ) {
    if ( $(element).find('.img-banner-content').length > 0 ) {
      $(element).addClass("has-content");
    }
  });

I'm missing something simple here and could do with a shake. The issue is it's adding the class "has-content" to all divs.

Reduced test case: JSFiddle

Thanks all.

Aucun commentaire:

Enregistrer un commentaire