I am looping through an array and displaying it on the screen. I have a problem, though. I tried to add a function that when you click on the text, it deletes that clicked text. Here is my code:
var div = document.querySelector('div');
var array = [
"Banana",
"Chocolate",
"Oranges"
];
for(let i = 0; i < array.length; i++){
var p = document.createElement('p');
p.textContent = array[i];
div.appendChild(p);
p.onclick = function(){
array.splice(array[i], 1);
console.log(array);
}
}
When I click on it, it deletes the item from the array and logs it to the console. But it doesn't show on the screen. Any help?
Thank you, Scratch Cat