Quantcast
Viewing latest article 19
Browse Latest Browse All 41

Answer by Just code for How to add a class in Javascript/jQuery if an element has content in it?

You can loop over parent div and find the child items and then add the class over there.

Here in the example, I am getting $('.featured-block__item-inner') as a parent and then finding items inside it.

$('.featured-block__item-inner').each(function() {  if ($(this).find(".featured-block__title").not(":empty").length > 0 && $(this).find(".featured-block__tag").not(":empty").length > 0) {    $(this).find(".img-fit img").addClass("opacity-pointseven"); // For condition#1  } else {    $(this).find(".img-fit img").addClass("default-opacity");  }})
.opacity-pointseven {  width: 100px;  height: 100px;}.default-opacity {  width: 50px;  height: 50px;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="featured-block"><a href="/" class="featured-block__item cf"><div class="featured-block__item-inner"><figure class="featured-block__image img-fit"><img src=""></figure><div class="featured-block__content"><h1 style="margin-bottom:0px;" class="featured-block__title">Trans Mountain Pipeline: NEB Releases New Report, Recommends Approval </h1><h1 class="featured-block__tag"> More Coverage</h1></div></div><div class="featured-block__item-inner"><figure class="featured-block__image img-fit"><img src=""></figure><div class="featured-block__content"><h1 style="margin-bottom:0px;" class="featured-block__title"></h1><h1 class="featured-block__tag"> More Coverage</h1></div></div></a></div>

Viewing latest article 19
Browse Latest Browse All 41

Trending Articles