0
<div v-if="result.length" style="clear:both">
      <div v-bind:key="item.id" v-for="item in result">
        <div class="ui form">
          <div class="field">
            <label>Content</label>
            <textarea name id cols="30" rows="10">{{item.content[0]}}</textarea>
          </div>
          <div class="field right aligned">
            <button v-bind:id="item.id" @click="updateData" class="ui primary button">Update</button>
          </div>
      </div>
</div>

have this iteration, id like to access to the value of 'current' text area on updateData function call. I thought assigning id to textarea input as $ref but seems kid of off. Ayn help is appreciated, thanks!

1
  • If you want to use $ref, you have to use ref="..." on the element, as shown here: [what's the real purpose of 'ref' attribute?](stackoverflow.com/questions/44063606/… ) Commented Feb 28, 2019 at 10:21

2 Answers 2

1

Use refs array

You need to use the index value of the v-for

And access to the refs by the index

<div v-bind:key="item.id" v-for="(item, index) in result">

<textarea ref="textarea" name id cols="30" rows="10">{{item.content[0]}}</textarea>

<button v-bind:id="item.id" @click="updateData($refs.textarea[index])" class="ui primary button">Update</button>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use databinding for the textareas, and pass the current item to your update funtion:

Vue.config.productionTip = false; // suppress

new Vue({
    el: "#app",
    data: {
        result: [
           {id:0,content:['some content']},
           {id:1,content:['some content']},
           {id:3,content:['some content']},
        ]
    },
    methods:{
        updateData(item){
            alert(item.content[0]);
        }
    }
    
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>


<div id="app">
  <div v-bind:key="item.id" v-for="item in result">
    <div class="ui form">
      <div class="field">
        <label>Content</label>
        <textarea name id cols="30" rows="10" v-model="item.content[0]"></textarea>
      </div>
      <div class="field right aligned">
        <button v-bind:id="item.id" @click="updateData(item)" class="ui primary button">Update</button>
      </div>
  </div>
  
</div>

1 Comment

Data binding with v-model, indeed! Thanks.

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.