Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda