1

Is there a way to store multiple values in data return when its been submitted.

View

<div id="app">
  <input type="text" v-model="inserted" placeholder="Insert Name">
  <button type="submit" v-on:click='submit();'>SUBMIT</button>
</div>

Script

new Vue({
  el: "#app",
  data: {
   inserted:[], /** each name submitted should be stored over here **/
   /** if the first name was submitted as DAVID and second name was submitted as JOHN **/
   /** inserted[] should consists of both values as ["DAVID", "JOHN"] **/
  },
  methods: {
    submit(){
    console.log('value submitted');
    }
  }
})

Below is my code on JSFIDDLE

https://jsfiddle.net/ujjumaki/ypbc2vf6/12/

2 Answers 2

2

Sure, just bind your input to a toInsert data property instead, and push it to inserted on submit:

<input v-model="inserted">
<button @click="submit">submit</button>
new Vue({
  data: {
    inserted: [],
    toInsert: '',
  },
  methods: {
    submit() {

      // save the value
      this.inserted.push(this.toInsert);

      // clean the input
      this.toInsert = '';

    }
  }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Took me longer then Nino but here is a working full example with some added validation so you do not add empty names:


You can create the input for some other kind of vue variable, f.e. name. In submit() you add that to your array if it is not empty:

new Vue({
  el: "#app",
  data: {
   inserted:[], /* each name submitted should be stored over here */
   /* if the first name was submitted as DAVID and second name was submitted as JOHN*/
   /* inserted[] should consists of both values as ["DAVID", "JOHN"] */
   name: "",
  },
  methods: {
  	submit() {
      if (this.name.length > 0) { 
        this.inserted.push(this.name);
    }
    }
  }
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

del {
  color: rgba(0, 0, 0, 0.3);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <input type="array" v-model="name" placeholder="Insert Name">
  <button type="submit" @click='submit();'>SUBMIT</button>
  
  <p>Message is: {{ inserted }}</p>
</div>

Comments

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.