For what you said one of the solutions would be this add onclick="removeimg(this)"
img when to show and create this new function
function removeimg(el){
//prgar src clicado
var src = el.src;
//prgar item do storege
storege =JSON.parse(localStorage.getItem('favorites'));
let newimg= false;
//foreache nos item
storege.forEach(img=>{
if(src != img){
if(!newimg){
newimg = [img];
console.log(newimg);
}else{
newimg.push(img);
}
}
});
//salvar as imagem que ele não clicou no favorito
localStorage.setItem('favorites', JSON.stringify(newimg))
atualizardiv();
}
changing the updateFavorites function
function updateFavorites() {
const { existsInLocalStorage, index, imageSource } = getState()
// existsInLocalStorage
// ? favorites.splice(index, 1)
// : favorites.push(imageSource)
let quantidade = favorites.length;
if(quantidade>0){
favorites.push(imageSource)
localStorage.setItem('favorites', JSON.stringify(favorites))
}else{
favorites=[imageSource];
localStorage.setItem('favorites', JSON.stringify(favorites))
}
}
//update the div when removing the image
function atualizardiv(){
$('.modal').innerHTML='';
$('.modal').innerHTML=' <h3>Clique <span id="remove">aqui</span> para remover todas as imagens</h3>'+
' <span class="X" draggable="true">×</span>';
storege =JSON.parse(localStorage.getItem('favorites'));
storege.forEach(img=>{
$('.modal').innerHTML= $('.modal').innerHTML+'<img src="'+img+'" onclick="removeimg(this)"/>';
});
}
the answer I gave helped you?
– clone por
I’ll check now
– bielb2
recalls that Voce has to add onclick="removeimg(this)" to the image thumbnails that appears in the div to choose which one to remove
– clone por
I did everything, but I’m not able to add onclick to the images, I tried to do it by innerHTML but does not recognize the function...
– bielb2
guy I managed to fix, it worked out the part of deleting each image, I’m just not getting to list all now.
– bielb2
as well as not listing ?
– clone por
Thanks even for the help, you gave me a basis for how to remove the images, only another problem happens. The way I created localStorage is different from the assignments you made below, what happens when you run the function is a conflict, and is giving error on the page due to other JSON definitions I did. I believe I have to fix the code to avoid these conflicts
– bielb2
watch the video to see what happens: https://ik.imagekit.io/b0g9wlasxh/Peek_27-08-2020_18-21_ECxuTASRg.mp4
– bielb2
by what I saw the error and at the time that vc da favorites.push na Function updateFavorites() what exactly you do on it?
– clone por
I just added an update to the answer by modifying its updateFavorites() function take a look and see if it goes
– clone por
I also edited the removeimg function, I noticed an error and corrected, try again
– clone por
nice, it worked!!! However I can only add 2 images in localStorage, does not let me add more.
– bielb2
I modified and fix the bug, put the remove and the updateFavorites and test, if it works check q the answer worked
– clone por
all right, thank you very much!!!
– bielb2
i added a Function q updates the div when you click on the image you want to remove, I was going to use the Empty however n was going through this ration I did so
– clone por
I tried it here, it was really good! I was giving Reload on the page every time I deleted some image, this code to update the div helped me a lot. The problem is that it disappears with my modal content (the X to close for example some)
– bielb2
updated function that updates the div and fix the bug that erased the fraze from the div I think it looks better from a glance
– clone por
was great, thanks!
– bielb2