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

165
Views
How to remove a particular item from an array?

My Dom is like this :

<div class="child">
  child1
  <button class="btn">Delete</button>
</div>
<div class="child">
  child2
  <button class="btn">Delete</button>
</div>
<div class="child">
  child3
  <button class="btn">Delete</button>
</div>
<div class="child">
  child4
  <button class="btn">Delete</button>
</div>

Now my js like this:

const btn = document.querySelectorAll('.btn');
btn.forEach((button, i) => {
  button.addEventListener('click', () => {
    const boxes = document.querySelectorAll('.child');
    boxes.splice(i, 1);
  });
});

Now boxes is an array and after clicking on delete button I want to remove The pertucal item from that arrray. but it throws the following error:

boxes.splice() is not a function

How can I solve this?

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

0

splice() is an array method. The collection returned from querySelectorAll() is Array-like. Use something like ... spread operator to convert it to an array.

const btn = document.querySelectorAll('.btn');
btn.forEach((button, i)=>{
    button.addEventListener('click', ()=>{
        const boxes = [...document.querySelectorAll('.child')];
        boxes.splice(i, 1)
    })
})

Note: This doesn't actually delete anything from the DOM. Only removes it from the array as you have indicated in the question.

about 4 years ago · Juan Pablo Isaza Report

0

The querySelectorAll returns a noed list You have to convert it to an array

const nodelist = document.querySelectorAll('div');
const nodelistToArray = Array.apply(null, nodelist);
about 4 years ago · Juan Pablo Isaza Report

0

Now boxes is an array and ...

Actually, boxes is NOT an array, as you think. boxes is the result of document.querySelectorAll, which returns a NodeList, not an array. A NodeList is very similar to an array, but it doesn't have all of the methods of Array.prototype. To use .splice on a NodeList, you have to convert it to an array. Here is how you would do that:

const btn = document.querySelectorAll('.btn');
btn.forEach((button, i) => {
  button.addEventListener('click', () => {
    const boxes = Array.from(document.querySelectorAll('.child'));
    console.log(boxes.splice(i, 1));
  });
});
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!