Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
Vue js show hide element on click

I am new in vue js. I want to show hide elements on click. So that What I can do - make a property in data object like

isHidden: false

and add a mehtod like

showHide(){
return this.isHidden = !this.isHidden;
}

And bind the showHide method on click event like

<li @click="showHide">Some text</a>

But the problem is- if there are several list items binding the same click event, all lists will be shown on click, or all will hide. But I want the target element will only show or hide by click.

How can I do this, please?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use some data to determine what element you want show/hide. For example: id, index of each element

<button v-for="(i, ind) in listElement" @click="toggleElement(ind)" :key="ind">
  Toggle {{ i }}
</button>

<p v-for="(i, ind) in listElementShow" :key="ind">
  Element: {{ i }}
</p>

Logic:

  data() {
    return {
      listElement: [ { name: 1, isShow: true}, { name: 2, isShow: true}],
    }
  },
  computed: {
    listElementShow() {
      return this.listElement.filter(e => e.isShow); 
    }
  }
  methods: {
    toggleElement(index) {
      this.listElement[index].isShow = !this.listElement[index].isShow;
    }
  }
about 4 years ago · Juan Pablo Isaza Report

0

You can do something like this:

<template>
    <ul class="list-with-hidden-items">
        <li
            v-for="item in list"
            :key="item.id"
            @click="showHide(item.id)"
        >
            {{ item.text }}
        </li>
    </ul>
</template>

<script>
export default {
    data: () => ({
        hidden: {}
    }),
    methods: {
        showHide(id) {
            this.$set(this.hidden, id, !this.hidden[id]);
        }
    }
}
</script>

and then you can render where you want your item to be shown like this:

<div v-if="!this.hidden.item1">I am shown because of item1 is not hidden</div>
about 4 years ago · Juan Pablo Isaza Report

0

You can use a dynamically bound classObject to do such a thing. See Class and Style Bindings

Sandbox

<template>
  <div @click="showHide()" :class="{ isHidden: isHidden }">
    <p>This is a thing</p>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      isHidden: false,
    };
  },
  methods: {
    showHide() {
      this.isHidden = !this.isHidden;
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.isHidden {
  background-color: grey;
}
div {
  background-color: #fff;
}
</style>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!