1

I am trying to toggle the class of another div on click.

Something like:

<div class="link">
 <p> Click here to show the content </p>
 <div class="content">
  <p>This is the hidden content</p>
 </div>
</div>

so content css should initially be: display: none

How can I do this on vue, when p is clicked, toggle the element below.

thnx in advance!

1 Answer 1

1

You don't need to use css classes for this if you use the v-show directive:

<div class="link">
  <p @click="show = true"> Click here to show the content </p>
  <div v-show="show" class="content">
    <p>This is the hidden content</p>
  </div>
</div>

In your Vue component, you just need to add a show property that is initially set to false:

data() {
  return {
    show: false,
  };
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for this... I didn't know this, but how to do conditional toggle now, because once I click it just shows, I want to be able to toggle on each click
thnx a lot for your help, I have another questions related.. I have many same div, about 10, and I want to be able once <div class="link"> is clicked, I want to show just it's content and not other related divs... is that possible ?
<div class="link"> <p @click="show = true"> Click here to show the content </p> <div v-show="show" class="content"> <p>This is the hidden content</p> </div> </div>
I would make this into it's own reusable component: vuejs.org/v2/guide/components.html

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.