4
$(document).ready(function() {
  if ($("#grid .media-box").hasClass("brand1")) {
    $("#grid .media-box-content").addClass("brand01")
  };
}
});

and in body looping div grid

<div id="grid">
  <?php foreach($data as $items) { ?>
  <div class="media-box video <?php  echo $items->brand; ?> <?php  echo $items->country; ?>">
    <div class="media-box-image">
      <div data-width="240" data-height="168" data-thumbnail="gallery/thumbnails/thumb-2.jpg"></div>
      <div data-type="iframe" data-popup="https://www.youtube.com/watch?v=5guMumPFBag" title="Psico dell consecteture"></div>
      <div class="thumbnail-overlay">
        <i class="fa fa-video-camera mb-open-popup"></i>
        <a href="#"><i class="fa fa-link"></i></a>
      </div>
    </div>
    <div class="media-box-content">
      <div class="media-box-title">Psico dell consecteture</div>
      <div class="media-box-date">
        <?php echo $items->country; ?></div>
      <div class="media-box-text">Lorem ipsum dolor sitam psico.</div>
      <div class="media-box-more"> <a href="#">Read more</a> 
      </div>
    </div>
  </div>
  <?php } ?>
</div>

CSS:

.media-box {
  font-size: 13px;
}
.brand01 {
  background: blue !important;
}
.media-box-content {
  padding: 20px;
  position: relative;
  color: rgb(51, 51, 51);
  line-height: 17px;
}

The above code is not working for me.

 <?php  echo $items->brand; ?> <?php  echo $items->country; ?>" >

is fetching 2 classes to the dive from database.

3 Answers 3

1
$("#grid .media-box.brand1").find(".media-box-content").addClass("brand01")

Your code will add class brand01 to all .media-box-content if the condition is true.

Sign up to request clarification or add additional context in comments.

Comments

0

There is no need to use if condition, you can use .toggleClass() with second arg as function which returns a boolean value as true/false:

$(document).ready(function() {
    $("#grid .media-box-content").toggleClass("brand01", function(){
       return $(this).closest(".media-box").hasClass("brand1");
    });
});

I feel you need to target the each .media-box-content and find the parent .media-box has the class, if has true then add it if false remove it.

Comments

0

Use this, it will work

$(document).ready(function() {
    $("#grid .media-box").each(function(index, element) {
        if ($(this).hasClass("brand1")) {
            $(this).children(".media-box-content").addClass("brand01")
          }
    });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.