Cannot Read property addEventListener of undefined?

var removeButton = document.getElementsByClassName('remove-btn')
for(var i = 0; i <= removeButton.length; i++){
  var button = removeButton[i]
  button.addEventListener('click',function(event) {
      var buttonClicked =

hello there,
I am very much new to javascript and the code that I have listed above should remove an item from the cart but my code does not enter the addEventListener method throwing back an error that says

Cannot Read property addEventListener of undeifined

can anybody help?

What does your HTML look like?

<div class="order-col">
								<div><strong>Product Total</strong></div>
							<div class="order-products">
								<div class="order-col">
										<img src="products/fig.jpg" alt=""><br>
										<button type="button" name="button" class="remove-btn">Remove</button>

this is my html

Js isn’t my strongpoint but shouldn’t you be looking for less than and not less than or equal to in your loop. Your loop starts at zero.


for(var i = 0; i < removeButton.length; i++)


so basic , no idea how is missed it ,
Thank you

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.