I tried to check out other answers on Stackoverflow and apply it to my own code but I couldn't get it to work.
I'm trying to implement the setup function that registers a click event handler and implements the following logic: when the button of class remove is clicked, its parent element should be removed from the list. It should be in pure javascript.
This is the original code:
function setup() {
//write your code here
}
document.body.innerTML = `
<div class="image">
<img src="https://" alt="First">
<button class="remove">X</button>
</div>
<div class="image">
<img src="https://" alt="Second">
<button class="remove">X</button>
</div>`;
setup();
document.getElementsByClassName("remove")[0].click();
console.log(document.body.innerHTML);
Here's my attempt
function setup() {
let buttondel = document.getElementsByClassName("remove");
buttondel.addEventListener('click',function(e){
e.currentTarget.parentNode.remove();
})
}
but I'm getting this error "buttondel.addEventListener is not a function". Here's my code at Codepen: https://codepen.io/kikibres/pen/PoZLbZa?editors=1011