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
-
2Why would it? You don't touch the paragraph element object.Quentin– Quentin2017年06月07日 20:11:58 +00:00Commented Jun 7, 2017 at 20:11
-
1you need to replace the contents of the "p" with the new array after deletionLiquidchrome– Liquidchrome2017年06月07日 20:12:13 +00:00Commented Jun 7, 2017 at 20:12
-
1Isnt it array.splice(i,1); ?Jonas Wilms– Jonas Wilms2017年06月07日 20:14:12 +00:00Commented Jun 7, 2017 at 20:14
-
Yes I know that but it doesn't work, it just deletes the last iteration.Scratch Cat– Scratch Cat2017年06月07日 20:20:29 +00:00Commented Jun 7, 2017 at 20:20
3 Answers 3
You seem to be close. You have removed the element from the array but not from the DOM, that is the reason why you cannot see the updated in the html.
for(let i = 0; i < array.length; i++){
var p = document.createElement('p');//note the block scoping
p.textContent = array[i];
div.appendChild(p);
p.onclick = function(){
array.splice(i, 1);
this.remove();//also remove from the DOM
console.log(array);
}
}
Dipesh KC
3,3173 gold badges35 silver badges50 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Jonas Wilms
you may hit the green checkmark somewhere as this queston is solved. however, taking over my comment in line two does not make sense (var is function scoping...)
Here you go:
var div = document.querySelector('div');
var array = [
"Banana",
"Chocolate",
"Oranges"
];
for (let i = 0; i < array.length; i++) {
let p = document.createElement('p'); //note the block scoping
p.textContent = array[i];
div.appendChild(p);
p.onclick = function() {
array.splice(array.indexOf(this), 1); //splice wants an index...
this.remove(); //remove el from document tree
console.log(array);
}
}
2 Comments
Scratch Cat
Don't worry I've fixed it. instead of using p.remove() I used this.remove(). Thank you.
for(let i = 0; i < array.length; i++){
let p = document.createElement('p');//note the block scoping
p.textContent = array[i];
div.appendChild(p);
p.onclick = function(){
array.splice(i, 1);//splice wants an index...
p.remove();//remove el from document tree
console.log(array);
}
}
You may also want to remove the html element?
answered Jun 7, 2017 at 20:15
Jonas Wilms
139k20 gold badges164 silver badges164 bronze badges
2 Comments
Scratch Cat
Yes I tried removing the paragraph but it didn't work. It just removed the last iteration.
Jonas Wilms
@Scratch Cat note line two... when using p.remove() its important that p is blockscoped. You could also use this.remove() instead, then it doesnt matter....
lang-js