How to get the innerText of rendered list item in VueJS

Viewed 4675

I want to get the innerText of an item in a rendered list, but accessing it using this.$refs doesn't seem to work. I've also tried to use v-modal and that doesn't seem to work either.

Here's my code:

<div id="simple" v-cloak>
  <h1>Clicked word value!</h1>
  <ul>
    <li v-for="word in wordsList" @click="cw_value" ref="refWord">
      {{ word }}
    </li>
    <h4> {{ clickedWord }} </h4>
  </ul>
</div>
var app = new Vue({
  el: '#simple',
  data: {
    clickedWord: '',
    wordsList: ['word 1', 'word 2', 'word 3']
  },
  methods: {
    cw_value: function() {
      this.clickedWord = this.$refs.refWord.innerText
      // "I don't know how to get inner text from a clicked value"
    }
  }
})
2 Answers

Since innerText takes CSS styles into account, reading the value of innerText triggers a reflow to ensure up-to-date computed styles. (Reflows can be computationally expensive, and thus should be avoided when possible.) Here is MDN document on that.

Now it is:

this.$refs.refWord[index].textContent
Related