multiple refs in a for loop #2950
Answered
by
SimoTod
jimmywarting
asked this question in
1. Help
-
when i use <template x-for="i in 10">
<div>
<details x-ref="detail">
<summary x-text="i"></summary>
</details>
<button x-on:click="$refs.detail.open = !$refs.detail.open" x-text="`open ${i}`" />
</div>
</template> demo: https://jsfiddle.net/fsgtehcb/1/ how can i solve this problem? |
Beta Was this translation helpful? Give feedback.
Answered by
SimoTod
May 22, 2022
Replies: 1 comment 1 reply
-
I wouldn't use x-ref. <template x-for="i in 10">
<div x-data="{open: false}">
<details x-effect="$el.open = open">
<summary x-text="i"></summary>
</details>
<button x-on:click="open = !open" x-text="`open ${i}`"></button>
</div>
</template> |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
jimmywarting
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wouldn't use x-ref.
X-ref usually means that you are trying to apply imperative code to Alpine which tends to be declarative (you don't change objects, you change a state and let all objects react to it). You can use something like