[Vuejs]-Vuejs: I need to reset the button back to the first name after pressing the button the third time

0👍

Hope this helps!!

new Vue({
  el: "#app",
  data(){
    return{
    testimonialData: [
      {
        name: 'W',
        comments: 'It was great.',
        stars: 5
      },
      {
        name: 'Tom',
        comments: 'Easy to use.',
        stars: 4
      },
      {
        name: 'Has',
        comments: 'Test',
        stars: 3
      }
    ],
    counter: 0
  }
  },
  methods:{
		increment(){
			if((this.testimonialData.length - 1) === this.counter) {
				this.counter = 0;
			}
			else {
			 this.counter++;
			}
		}
  },
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<div id="app">
  <h3>{{testimonialData[counter].comments}}</h3>
  <hr>
  <button @click="increment">Increment</button>
</div>

0👍

Have a look at this.

new Vue({
  el: "#app",
  data() {
    return {
      testimonialData: [{
          name: 'W',
          comments: 'It was great.',
          stars: 5
        },
        {
          name: 'Tom',
          comments: 'Easy to use.',
          stars: 4
        },
        {
          name: 'Has',
          comments: 'Test',
          stars: 3
        }
      ],
      counter: 0
    }
  },
  methods: {
    increment() {
      //for generic if(this.counter === this.testimonialData.length - 1)
      if (this.counter == 2)
        this.counter = 0;
      else
        this.counter++;

    }
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
  <button class="next" @click="increment"> > </button>
  <h3>{{testimonialData[counter].name}}</h3>
  <h4>"{{testimonialData[counter].comments}}" with
    <span>{{testimonialData[counter].stars}} star/s.</span>
  </h4>
</div>

Leave a comment