0

I was trying to build a shopping cart with Vanilla javascript and HTML &CSS. I have run into a problem where by I could not remove an item form the shopping cart. I have been using the Local storage. When I click on the remove icon I get this error:

myown.js:171 Uncaught TypeError: Cannot read property 'getElementsByClassName' of undefined at HTMLElement.

I have tried many things but it seems hard to get around. Any help will be appreciated.

My code:

function deleteButtons(){

   let deleteButtons =document.querySelectorAll('.product ion-icon ');
   console.log("deleteButtons ");

   let productName;
   for (i=0; i< deleteButtons.length; i++) {
        deleteButtons[i].addEventListener('click', () =>{
            console.log("clicked");
            // productName = deleteButtons[i].parentElement;
            productName = deleteButtons[i].parentElement.textContent;
            console.log(productName );       
        });
   }
}
Paul T.
  • 4,703
  • 11
  • 25
  • 29
  • Mind sharing the code relevant to removing item from the cart? – sid Jul 07 '21 at 02:27
  • You're getting that error because your script can not find the HTML element which you're trying to get by a specific class name. Either the element does not exist on the DOM itself, or the element does not have the class or it has been removed before that call happens. You will need to go through and check your HTML for that specific class name in order for the script to run correctly. – Chris Jul 07 '21 at 02:28
  • In the code example, there is neither `getElementsByClassName` or `localStorage` present? – Paul T. Jul 07 '21 at 02:28

0 Answers0