Delete object from array on click

AntunCrnja

I need help with deleting an object from the array.

The names(array.name) from the objects are displayed in the DOM and I want to delete them using the erase X button .

In this code I can only delete one element and then stops working with no console error. Does anyone have an idea where I am wrong and why I can't call the create() function every time I click on an erase button.

Look at console.log... it shows the good position and name of the element to be deleted, but everything stops when I call create() function, try uncomment create().

const array = [ {name: 'John'}, {name: 'David'}, {name: 'Peter'}, {name: 'Michael'} ]; 

const create = () => {
    const app = document.querySelector('.app');
    app.innerHTML = '';

    for(let i = 0; i < array.length; i++) {
      app.innerHTML += `<div class="item"><span class="erase">&#9747;</span>${array[i].name}</div>`;
    }
}
create();

const erase = document.querySelectorAll('.erase');

erase.forEach(item => {
    item.onclick = () => {
      const itemText = item.parentElement.textContent.substr(1);
      const itemPos = array.findIndex(item => item.name == itemText);
    
        console.log(itemText + ' ' + itemPos);
        console.log(array);
        array.splice(itemPos, 1);
        //create()
    }
});
.erase {
  color: red;
  margin-right: 20px;
  cursor: pointer;
}

.item {
  margin-bottom: 10px;
}
<div class="app"></div>

Or as fiddle: https://jsfiddle.net/05cwy9d2/3/

Abror Abdullaev

Its because when you create new elements in the dom the listeners of old elements are being removed together with the old elements. Just move the event listener registration into create method.

const array = [
  {name: 'John'},
  {name: 'David'},
  {name: 'Peter'},
  {name: 'Michael'}
]; 

const create = () => {
  const app = document.querySelector('.app');
  app.innerHTML = '';

  for(let i=0;i<array.length;i++){
    app.innerHTML += 
      `<div class="item"><span class="erase">&#9747;</span>${array[i].name}</div>`;
  }

  const erase = document.querySelectorAll('.erase');

  erase.forEach(item => {
    item.onclick = () => {
      const itemText = item.parentElement.textContent.substr(1);
      const itemPos = array.findIndex(item => item.name == itemText);


      console.log(itemText + ' ' + itemPos);
      console.log(array);
      array.splice(itemPos, 1);
      create();
    }
  });
}

create();
.erase{
color: red;
margin-right: 20px;
cursor: pointer;
}
.item{
margin-bottom: 10px;
}
<div class="app"></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete object from array

From Dev

Click on an element and delete it from an array

From Dev

JavaScript - Delete Array from Object

From Dev

How to delete object from array?

From Dev

Delete an object from array React

From Dev

How to delete and object from an array

From Dev

Delete object from array in mongoose

From

How do I delete an item or object from an array using ng-click?

From Dev

Delete an entry from array of objects on button click

From Dev

How to delete from the array, with date id, and click?

From Dev

How to delete object from object array?

From Dev

Delete object or nested object from array

From Dev

Deleting an object from an array on click of a button

From Dev

How to delete object from array in firestore

From Dev

Delete object by id from array Ramda

From Dev

Find and delete Object from Array by Key Value

From Dev

Delete specific value from Array in Object

From Dev

Delete object from an array if the key value match

From Dev

Angular - delete object from array completely

From Dev

Delete object from array by given _id in in Mongoose

From Dev

Delete duplicate rows from Multidimensional Object array [,]?

From Dev

Parse Android: delete object from array

From Dev

Javascript - delete object from JSON array

From Dev

Delete an object from a nested array of objects

From Dev

Delete last object from the array of objects.

From Dev

How to correctly delete an object from an array by name?

From Dev

Find and delete a json object from JSON array

From Dev

Delete object from array using map

From Dev

Moving array of object from one array to another array on button click

Related Related

HotTag

Archive