[Vuejs]-Binding div value to event handler

1👍

<div id="app">
  {{value}}
  <div @click="getValue">1</div>
  <div @click="getValue">2</div>
</div>
new Vue({
  el: 'app',
  data: {
    value: ''
  },
  methods: {
    getValue: (event) => this.value = event.target.textContent
  }
})

1👍

If the actual content of the div is hardcoded, I guess that the call will be hardcoded too…

You could try:

<div id="app">
  {{value}}
  <div v-for="val in values" @click="getValue(val)">{{ val }}</div>
</div>

And then:

new Vue({
  el: 'app',
  data: {
    value: '',
    values: [1, 2]
  },
  methods: {
    getValue: (v) => this.value = v
  }
})

Leave a comment