0

I'm making a menu, with some accordion links, that when clicked, will show more options. The problem now, is that when I click one, they all trigger.

I need to trigger only the element that has been clicked.

I've tried @click.prevent, stop, self etc and not working. Maybe I'm missing something??

<li :aria-expanded="foo">
    <button id="foo-1" @click="foo = !foo" :aria-pressed="foo" :aria-expanded="foo" >
    FOO
    </button>
    <ul v-show="foo">
        <li> Accordion option </li>
     </ul>
</li>
<li :aria-expanded="foo">
    <button  id="foo-2" @click="foo = !foo" :aria-pressed="foo" :aria-expanded="foo" >
                FOO
    </button>
    <ul v-show="foo">
        <li> Accordion option </li>
     </ul>
</li>

I'm using foo as boolean type.

Any idea??

Thanks you!!

0

1 Answer 1

1

The problem is all the <li>s are bound to the same foo variable, so editing one reference affects all others.

The solution is to create separate variables for each <li>. For example, you could change foo to be an array, and bind each individually:

<template>
  <li>
    <button @click="foo[0] = !foo[0]">FOO</button>
  </li>
  <li>
    <button @click="foo[1] = !foo[1]">FOO</button>
  </li>
<template>

<script>
export default {
  data() {
    return {
      foo: []
    }
  }
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.