[Vuejs]-How to update a textarea with vue.js based on single inputs from textarea

0👍

with the v-for Vue will duplicate the element for any array item.
I suggest to generate the textarea content directly in the computed property.
Here below a working example.

var app = new Vue({
    el: '#app',
    data: {
        address: '',
    },
    methods: {

    },
    computed: {
        domainlist() {
            var addreses = this.address.split('\n');
            var ret = "";
            for(var addr in addreses) {
              ret += addreses[addr] ? 'domain: '+addreses[addr]+"\n":"";
            }
            return ret;
        }
    }
})
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>.com.np Cover Letter Generator</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css">
</head>

<body>
    <div id="app">
        <section class="section">
            <div class="container">
                <div class="columns is-centered">
                    <div class="column is-half">
                        <div class="box">
                            <form>
                                <div class="field ">
                                    <label class="label is-size-5 ">Domain Name</label>
                                    <div class="control">
                                        <textarea class="textarea is-medium " placeholder="Ex: www.abc.com.np " v-model="address " required="required"></textarea>
                                    </div>
                                </div>


                            </form>
                        </div>
                    </div>
                    <div class="column is-half">
                        <div class="box">

                            <div class="field">
                                <label class="label is-size-5">Output</label>
                                <div class="control">
                                    <textarea class="textarea" placeholder="Result" readonly>{{domainlist}}
                                    </textarea>

                                </div>
                            </div>

                            <div class="field">
                                <div class="control buttons is-centered ">
                                    <button class="button is-primary is-medium ">Copy</button>
                                    <button class="button is-danger is-medium ">Download</button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

            </div>
        </section>
    </div>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
 
</body>

</html>

0👍

Replace

return this.address.split('\n')

With

return this.address.split('.') // so that a.b.c becomes ['a','b','c']

You can also move the v-for out of the textarea to the <div class="control"></div>

  <div class="control" v-for="address in domainlist">
      <textarea class="textarea" placeholder="Result" readonly>domain:{{address}}
      </textarea>
   </div>
var app = new Vue({
    el: '#app',
    data: {
        address: '',
    },
    methods: {

    },
    computed: {
        domainlist() {
            return this.address.split('.')
        }
    }
})
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>.com.np Cover Letter Generator</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css">
</head>

<body>
    <div id="app">
        <section class="section">
            <div class="container">
                <div class="columns is-centered">
                    <div class="column is-half">
                        <div class="box">
                            <form>
                                <div class="field ">
                                    <label class="label is-size-5 ">Domain Name</label>
                                    <div class="control">
                                        <textarea class="textarea is-medium " placeholder="Ex: www.abc.com.np " v-model="address " required="required"></textarea>
                                    </div>
                                </div>


                            </form>
                        </div>
                    </div>
                    <div class="column is-half">
                        <div class="box">

                            <div class="field">
                                <label class="label is-size-5">Output</label>
                                <div class="control" v-for="address in domainlist">
                                    <textarea class="textarea" placeholder="Result" readonly>domain:{{address}}
                                    </textarea>

                                </div>
                            </div>

                            <div class="field">
                                <div class="control buttons is-centered ">
                                    <button class="button is-primary is-medium ">Copy</button>
                                    <button class="button is-danger is-medium ">Download</button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

            </div>
        </section>
    </div>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
 
</body>

</html>

Leave a comment