0

Using framework7, I would like to create a cart view on my application following you-tube tutorial.

I checked that the js file is properly called but the button is not working. If I click the button, console.log('clicked') should be displayed, but It doesn't work.

https://github.com/hahmmj/IPP20201_HALFSTACK

Javascript code:

console.log("storejs")

window.onload = () => {
var removeCartItemButtons = document.getElementsByClassName('btn-danger');
console.log(removeCartItemButtons)
for (var i = 0; i < removeCartItemButtons.length; i++){
    var button = removeCartItemButtons[i]
    button.addEventListener('click', function(){
        console.log('clicked')
    })
}
}
Christian
  • 4,902
  • 4
  • 24
  • 42
  • [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – charlietfl Jan 19 '21 at 01:22
  • It seems to work for me. On a blank page I put the JS given into ` – Dcdanny Jan 19 '21 at 01:52
  • Maybe ensure there are no transparent elements covering the buttons – Dcdanny Jan 19 '21 at 01:55
  • Thanks @Dcdanny, The problem is that other button is working and seems to be clicked properly. But when i click this button to remove articles (with class name btn-danger), It looks like other place is clicked (even if i clicked the button) – moonju Hahm Jan 19 '21 at 02:13

1 Answers1

-1

This looks similar to this: JavaScript closure inside loops – simple practical example which has several solutions to your problem.

Essentially as the function within addEventListener is asynchronous, it only runs after the loop has completed.

Dcdanny
  • 469
  • 2
  • 8