Home > OS >  How to update a textarea with vue.js based on single inputs from textarea
How to update a textarea with vue.js based on single inputs from textarea

Time:02-01

In VueJS I am using textarea to get input from the user and also use another textarea to show output. Here, everything is working except I am getting multiple outputs with textarea like this.

I am using v-for in textarea as the code is perfectly working if I use inside ordered/unordered list.

Image: Multiple Textarea box while output

Code

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

    },
    computed: {
        domainlist() {
            return this.address.split('\n')
        }
    }
})
<!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/[email protected]/css/bulma.min.css">
</head>

<body>
    <div id="app">
        <section >
            <div >
                <div >
                    <div >
                        <div >
                            <form>
                                <div >
                                    <label >Domain Name</label>
                                    <div >
                                        <textarea  placeholder="Ex: www.abc.com.np " v-model="address " required="required"></textarea>
                                    </div>
                                </div>


                            </form>
                        </div>
                    </div>
                    <div >
                        <div >

                            <div >
                                <label >Output</label>
                                <div >
                                    <textarea  placeholder="Result" v-for="address in domainlist" readonly>domain:{{address}}
                                    </textarea>

                                </div>
                            </div>

                            <div >
                                <div >
                                    <button >Copy</button>
                                    <button >Download</button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

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

</html>

CodePudding user response:

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/[email protected]/css/bulma.min.css">
</head>

<body>
    <div id="app">
        <section >
            <div >
                <div >
                    <div >
                        <div >
                            <form>
                                <div >
                                    <label >Domain Name</label>
                                    <div >
                                        <textarea  placeholder="Ex: www.abc.com.np " v-model="address " required="required"></textarea>
                                    </div>
                                </div>


                            </form>
                        </div>
                    </div>
                    <div >
                        <div >

                            <div >
                                <label >Output</label>
                                <div >
                                    <textarea  placeholder="Result" readonly>{{domainlist}}
                                    </textarea>

                                </div>
                            </div>

                            <div >
                                <div >
                                    <button >Copy</button>
                                    <button >Download</button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

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

</html>

CodePudding user response:

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 ></div>

  <div  v-for="address in domainlist">
      <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/[email protected]/css/bulma.min.css">
</head>

<body>
    <div id="app">
        <section >
            <div >
                <div >
                    <div >
                        <div >
                            <form>
                                <div >
                                    <label >Domain Name</label>
                                    <div >
                                        <textarea  placeholder="Ex: www.abc.com.np " v-model="address " required="required"></textarea>
                                    </div>
                                </div>


                            </form>
                        </div>
                    </div>
                    <div >
                        <div >

                            <div >
                                <label >Output</label>
                                <div  v-for="address in domainlist">
                                    <textarea  placeholder="Result" readonly>domain:{{address}}
                                    </textarea>

                                </div>
                            </div>

                            <div >
                                <div >
                                    <button >Copy</button>
                                    <button >Download</button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

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

</html>

  •  Tags:  
  • Related