cocoon
cocoon copied to clipboard
element.dispatchEvent(new Event(event)) doesn't work
Here's a simple example:
document.getElementById("input-field1").addEventListener("cocoon:after-insert", () => {
document.getElementById("input-field2").dispatchEvent(new Event("input"));
})
The dispatchEvent doesn't work on any cocoon callback.
A workaround is to place the dispatchEvent inside a setTimeout function
document.getElementById("input-field1").addEventListener("cocoon:after-insert", () => {
setTimeout(() => {
document.getElementById("input-field2").dispatchEvent(new Event("input"))
}, "5");
})
Not sure if that's an expected behavior or a bug.
The original javascript uses jquery to dispatch and handle events, so imho that is not compatible, but probably you are not using the "original" javascript (there is a version using "plain" javascript).
So assuming you are using that version, the events are ment to be handled on a container level, they will not be dispatched on any single input-field inside the form.