Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Adding class to only one div

I need some help adding a class to a div if is has an image to it.

<div class="large-6 columns check-Div">
  <div class="custom-table">
    <div class="text">
      <?php echo $latestimage; ?>
    </div>
    <div class="custom-table-box">
      <?php echo $table; ?>
    </div>
  </div><!-- end custom-table -->
</div>
if ($j(".large-6.columns.check-Div > .custom-table-box:contains('size-full')")) {
  $j('.large-6.columns.check-Div').addClass("hasImage");
}

The jQuery above is adding the hasImage class to all of the divs but I only want it to apply when there is a div that contains a image. Is there a way to do it singularly? Or by parent? I have tried but I'm a little lost.

Any help would be wonderful!

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

CSS :has selector is what you are looking for (here for a direct descendant)

$('div:has(> img)').addClass("hasImage") //jquery way

or just in css (but this would require a polyfill, as its in Level 4, which is draft currently)

div:has(> img) {
   border-color: 1px solid red;
}

https://developer.mozilla.org/en-US/docs/Web/CSS/:has

JSfiddle here: https://jsfiddle.net/y6gtt2vg/1/


Browser support

About CSS Selector Compatibility

Regardless of a browser's support of CSS selectors, all selectors listed at api.jquery.com/category/selectors/ will return the correct set of elements when passed as an argument of the jQuery function.

https://jquery.com/browser-support/

about 4 years ago · Santiago Trujillo Report

0

You can do this by removing the if statement and just adding the class based on the :has selector:

$j('.large-6.columns.check-Div:has(img)').addClass('hasImage');
about 4 years ago · Santiago Trujillo Report

0

You can do this using jquery no need to add if condition

jQuery(".custom-table-box:has(img)").parents(".check-Div").addClass("hasImage");
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!