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

237
Views
How to remove div if img src is empty with javascript

Hi I am trying to figure out how to remove a div if the img src is empty.

I've searched on stackoverflow, but most are all jq based. Can someone help in vanilla javascript?

<div class="swiper-wrapper ">
      <div class="swiper-slide">
          <img
             src="https://images.unsplash.com/photo-1526947425960-945c6e72858f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTN8fHByb2R1Y3RzfGVufDB8fDB8fA%3D%3D&w=1000&q=80"
             alt=""
             class="imgCard"
             color=""
           />
       </div>
       <div class="swiper-slide">
          <img
             src=""
             alt=""
             class="imgCard"
             color=""
           />
       </div>
</div>
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Here how you can do this

const swipers = document.querySelectorAll('.swiper-slide');

swipers.forEach(e => {
  const imgSource = e.querySelector('img').getAttribute('src');
  if (imgSource.trim() === '') {
    e.remove()
  }
})
<div class="swiper-wrapper ">
  <div class="swiper-slide">
    <img src="https://images.unsplash.com/photo-1526947425960-945c6e72858f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTN8fHByb2R1Y3RzfGVufDB8fDB8fA%3D%3D&w=1000&q=80" alt="" class="imgCard" color="" />
  </div>
  <div class="swiper-slide">
    <img src="" alt="" class="imgCard" color="" />
  </div>
</div>

over 4 years ago · Santiago Trujillo Report

0

You can get all images which are child of .swiper-slide class and check if their have src attribute different than an empty string like this

let imgs = document.querySelectorAll(".swiper-slide img");

imgs.forEach(item => {
  if (item.getAttribute('src') === "") {
    item.parentNode.remove();
  }
});
<div class="swiper-wrapper ">
  <div class="swiper-slide">
    <img src="https://images.unsplash.com/photo-1526947425960-945c6e72858f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTN8fHByb2R1Y3RzfGVufDB8fDB8fA%3D%3D&w=1000&q=80" alt="First image alternate text" class="imgCard" color="" />
  </div>
  <div class="swiper-slide">
    <img src="" alt="Second image alternate text" class="imgCard" color="" />
  </div>
</div>

over 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!