Delete the clicked image from the Storage location

Asked

Viewed 81 times

0

Basically I made a system to favorite images by clicking to save in Storage location.

let favorites = JSON.parse(localStorage.getItem('favorites')) || []

localStorage.setItem('favorites', JSON.stringify(favorites))

I was able to list all the favorite images on a page, scrolling through them and adding an onclick function for each, and wanted to remove only the image that I click.

follow full code: https://codepen.io/bielb2/pen/dyMWmMa

  • the answer I gave helped you?

  • I’ll check now

  • recalls that Voce has to add onclick="removeimg(this)" to the image thumbnails that appears in the div to choose which one to remove

  • 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...

  • guy I managed to fix, it worked out the part of deleting each image, I’m just not getting to list all now.

  • as well as not listing ?

  • 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

  • watch the video to see what happens: https://ik.imagekit.io/b0g9wlasxh/Peek_27-08-2020_18-21_ECxuTASRg.mp4

  • by what I saw the error and at the time that vc da favorites.push na Function updateFavorites() what exactly you do on it?

  • I just added an update to the answer by modifying its updateFavorites() function take a look and see if it goes

  • I also edited the removeimg function, I noticed an error and corrected, try again

  • nice, it worked!!! However I can only add 2 images in localStorage, does not let me add more.

  • I modified and fix the bug, put the remove and the updateFavorites and test, if it works check q the answer worked

  • all right, thank you very much!!!

  • 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

  • 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)

  • 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

  • was great, thanks!

Show 13 more comments

1 answer

1


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">&times;</span>';
    storege =JSON.parse(localStorage.getItem('favorites'));
    storege.forEach(img=>{
     $('.modal').innerHTML= $('.modal').innerHTML+'<img src="'+img+'" onclick="removeimg(this)"/>';
     });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.