I’m trying to hide a div after it has been faded out but I’m having a hard time removing the event listened once fired up.
import { Controller } from "stimulus"
export default class extends Controller {
static targets = ["field", "displayFieldButton", "hideFieldButton"]
connect() {
this.fieldTarget.classList.add("is-hidden")
this.hideFieldButtonTarget.classList.add("is-hidden")
}
displayField() {
this.fieldTarget.classList.toggle("is-hidden")
this.fieldTarget.classList.add("fadeIn")
this.displayFieldButtonTarget.classList.toggle("is-hidden")
this.hideFieldButtonTarget.classList.toggle("is-hidden")
}
hideField() {
this.fieldTarget.classList.remove("fadeIn")
this.fieldTarget.classList.add("fadeOut")
console.log('before addevent')
this.fieldTarget.addEventListener(this.animationEnd(), event => {
this.removeField()
}, false)
console.log('after addevent')
this.hideFieldButtonTarget.classList.toggle("is-hidden")
this.displayFieldButtonTarget.classList.toggle("is-hidden")
}
removeField() {
this.fieldTarget.removeEventListener(this.animationEnd(), event => {
this.removeField()
}, false)
this.fieldTarget.classList.add("is-hidden")
console.log('after removing')
}
animationEnd() {
console.log('nimating')
var animations = {
animation: "animationend",
OAnimation: "oAnimationEnd",
MozAnimation: "mozAnimationEnd",
WebkitAnimation: "webkitAnimationEnd",
};
let elem = document.createElement('div')
for (var t in animations) {
if (elem.style[t] !== undefined) {
console.log(animations[t])
return animations[t]
}
}
}
}
Everything works fine the first time I run hideField
but if I call it a second time, removeField
is called right after displayField
.
Why removeEventListener
isn’t working?