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

346
Views
How to hide parent div if first child is empty

I want to hide class "aa" if the first div element is empty in class "ca". Please guide me

My sample code:

<div class="aa">
<div class="ca">
   <div></div>
   <div class="cb"></div>
   <div class="cc"></div>
</div>
<div>

Appreciate your response!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can check to see if the first childNode of the .ca parent element has content, and then target .aa from there if it does not:

//If there is no content within the first div of the class="ca" div...
//choose between textContent and innerHTML below 
//based on whether it is going to have any text or some HTML within (with possibly no text)
if (!document.querySelector('.ca').childNodes[0].innerHTML) {
    //hide the class="aa" div
    document.querySelector('.aa').style.display = 'none';
}
about 4 years ago · Juan Pablo Isaza Report

0

You can use children(":first") to get first child of div as

$(document).ready(function(){
if($('.ca').children(":first").text() == ""){
  $('.aa').hide();
}
});

$(document).ready(function(){
if($('.ca').children(":first").text() == ""){
  $('.aa').hide();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="aa">
<div class="ca">
   <div></div>
   <div class="cb">A</div>
   <div class="cc">B</div>
</div>
<div>

about 4 years ago · Juan Pablo Isaza 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!