Dispatch custom event to <template> clone element #4405
-
I have elements (input fields wrapped in a div structure) which can be duplicated dynamically. I do this using a
and
But the component never receives the event. I have tried sending the event to the parent element (with an |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
You are dispatching the event too early, you need to allow Alpine to initialise the component, javascript is a single-threaded language and can only do one thing at the time. Use nextTick to queue the event after the component has been created. |
Beta Was this translation helpful? Give feedback.
-
Is there a reason you're not using x-for? |
Beta Was this translation helpful? Give feedback.
You are dispatching the event too early, you need to allow Alpine to initialise the component, javascript is a single-threaded language and can only do one thing at the time. Use nextTick to queue the event after the component has been created.
You can access data directly by using Alpine.$data as well (again, you need to wait one tick).
See https://codepen.io/SimoTod/pen/abewqgz