0

So I am having a problem with deleting items from an array while in a for loop

var div = document.querySelector('div');
var array = [
"iPad",
"iPod",
"iPhone"
];

for (let i = 0; i < array.length; i++) {
  let p = document.createElement('p');
  p.textContent = array[i];
  div.appendChild(p);

  p.onclick = function() {
    array.splice(array.indexOf(this, 1)); 
    this.remove(); 
    console.log(array);
  }

}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
  </head>
  <body>
    
    <div>
      
    </div>
    
  </body>
</html>


So the problem is that when I click on an item to delete it, it doesn't delete that specific one. No matter which one I click on, it deletes starting from the bottom ("iPhone" to "iPod" to "iPad").

Visually, it deletes the right one but when I go to the console I can see that it deletes from the bottom up. Any help would be so very much appreciated.

2
  • provided link is not working Commented Jul 7, 2017 at 6:42
  • @SyedDaniyalAsif The cake is a lie Commented Jul 7, 2017 at 6:52

4 Answers 4

1
for (let i = 0; i < array.length; i++) {
  let p = document.createElement('p'),
      a = array[i];
  p.textContent = a;
  div.appendChild(p);

  p.onclick = function() {
    array.splice(array.indexOf(a),1); 
    this.remove(); 
    console.log(array);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

you are using array.splice(array.indexOf(this, 1)) wrong, this gives you the clicked element where as array contains text inside that element, you need to do something like this:

var div = document.querySelector('div');
var array = [
"iPad",
"iPod",
"iPhone"
];

for (let i = 0; i < array.length; i++) {
  let p = document.createElement('p');
  p.textContent = array[i];
  div.appendChild(p);

  p.onclick = function() {
    array.splice(array.indexOf(this.innerHTML),1); 
    this.remove(); 
    console.log(array);
  }

}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
  </head>
  <body>
    
    <div>
      
    </div>
    
  </body>
</html>

Comments

0

In the onclick function make the following changes.

p.onclick = function() {
    array.splice(array.indexOf(this.textContent),1); 
    this.remove(); 
    console.log(array);
  }

Comments

0

please have a look of this to this.innerHTML

var div = document.querySelector('div');
var array = [
"iPad",
"iPod",
"iPhone"
];

for (let i = 0; i < array.length; i++) {
  let p = document.createElement('p');
  p.textContent = array[i];
  div.appendChild(p);

  p.onclick = function() {
array.splice(array.indexOf(this.innerHTML), 1); 
this.remove(); 
console.log(array);
  }

}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
  </head>
  <body>
    
    <div>
      
    </div>
    
  </body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.