Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
How to bind value to each list item in Vue to toggle the images

I'm trying to toggle between the 2 images by triggering a function on click event, as shown below, how do I apply the same idea to different list items to toggle the images individually for each item? right now, all the list items same image because of one global value i.e val

var app = new Vue({
  el: '#app',
  data: function() {
    return {
      val:true,
        selectedImg:"",
        checked: "@/assets/images/check.png",
        unchecked: "@/assets/images/uncheck.png",
        items: [
           { message: 'laundry' },
           { message: 'cooking' },
           { message: 'cleaning'}
          ]
    }
  },

  methods: {
   myfunc () {
          this.val = (this.val === true ? false : true) 
           
          if(this.val === true) {
            this.selectedImg = this.checked
          }  else  {
            this.selectedImg = this.unchecked
          }
      },
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
   <ul>
     <li v-for="item in items" :key="item.message">
       <button @click="myfunc"><img :src="selectedImg"/></button>
         {{ item.message }}  
     </li>
   </ul>
   
</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should move selectedImg and val into items objects and pass item to myfunc function. You can also look at the answer in codepen.

var app = new Vue({
  el: '#app',
  data: function() {
    return {
        checked: "@/assets/images/check.png",
        unchecked: "@/assets/images/uncheck.png",
        items: [
           { message: 'laundry', selectedImg:"" , val:true},
           { message: 'cooking', selectedImg:"", val:true},
           { message: 'cleaning', selectedImg:"", val:true}
          ]
    }
  },

  methods: {
   myfunc (item) {
          item.val = (item.val === true ? false : true) 
           
          if(item.val === true) {
            item.selectedImg = this.checked
          }  else  {
            item.selectedImg = this.unchecked
          }
      },
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
   <ul>
     <li v-for="item in items" :key="item.message">
       <button @click="myfunc(item)"><img :src="item.selectedImg"/></button>
         {{ item.message }}  
     </li>
   </ul>
   
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda