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

174
Views
Delete only div containers inside a container

The aim is to delete all div containers within a container and leave two anhchor tags untouched.

<div id="container">
  <div id="wrapper" class="slider">

    <div><img src=""></div>
    <div><img src=""></div>
    <div><img src=""></div>

    <!-- Next and previous buttons -->
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>

  </div>
</div>

expected / goal

<div id="container">
  <div id="wrapper" class="slider">
    <!-- Next and previous buttons -->
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>

  </div>
</div>

With innerHTML everything is deleted and therefore also the a tags. Is there an elegant and simple solution?

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

0

Use the remove method to remove an element from the DOM.

document.querySelectorAll("#wrapper > div").forEach(el => el.remove());
about 4 years ago · Juan Pablo Isaza Report

0

You can use querySelectorAll to find all div blocks:

let container = document.getElementById("wrapper");
container.querySelectorAll("div").forEach(e => e.remove());

Note: I haven't tested this on a document with nested divs. Since then one div could get deleted before it is iterated in the loop it may cause problems. And additional check or repeating querySelectorAll until none are found should solve that problem.

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!