1

It is strange. I am trying to trigger a method when an element is clicked in the v-for loop. But it does not work when I use v-if or v-show. This is my HTML code sample;

 <div class="chosen-drop custom_choices" v-if="showResults"> <!--   -->
            <ul class="chosen-results">
                <li class="active-result level-0 isresult" v-for="City in Results" v-bind:class="{ highlighted: SelectCity.name==City.name }" v-on:click="HandleSelectCity(City)" >{{ City.name }}</li>
            </ul>
 </div>

This is my method;

            methods: {
                HandleSelectCity: function (City){
                    this.SelectCity = City;
                    this.search_input = City.name;
                }
            },

I am using Vuejs 1.0.8

2 Answers 2

2

This works as expected for me.

new Vue({
  el: '#app',
  data: {
    showResults: true,
    Results: [{
        name: 'one'
      },
      {
        name: 'two'
      }
    ],
    SelectCity: null
  },
  methods: {
    HandleSelectCity(City) {
      this.SelectCity = City;
      this.search_input = City.name;
    }
  }
});
.highlighted {
  background-color: yellow;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/1.0.8/vue.min.js"></script>
<div id="app" class="chosen-drop custom_choices" v-if="showResults">
  <ul class="chosen-results">
    <li class="active-result level-0 isresult" v-for="City in Results" v-bind:class="{ highlighted: SelectCity.name==City.name }" v-on:click="HandleSelectCity(City)">{{ City.name }}</li>
  </ul>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I solved the problem finally. This was just a conflict between the v-on:blur and v-on:click
1

I have solved the problem. This was just a conflict between the v-on:click and v-on:blur When the element that had v-if was removed the user didn't have any time to click on the element with the v-on:click

I solved the problem by adding a delay.

1 Comment

Could u give a demo? think you. I dont understand thsi way. I have this problem.

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.