M
M
MrJobs2020-05-04 19:22:01
JavaScript
MrJobs, 2020-05-04 19:22:01

How to find the last div and remove all others?

How can I find the last divone and delete all the others?

<div id='container'>
   <div>block_1</div>
   <div>block_2</div>
   <div>block_3</div>
   <div>block_4</div>
   <div>block_5</div>
</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
y0u, 2020-05-04
@MrJobs

const div = document.querySelectorAll('#container > div:not(:last-child)');
div.forEach(el => el.remove());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question