I'm trying to grab the value from a select option and pass it to the input field name so I can create different input fields on button click.
So far I have an array of input fields but I'm not sure how to grab the select option value and output this within the input name field, resetting it each button click.
Markup
<div id="app">
<ul>
<li v-for="(input, index) in inputs">
<input type="text" v-model="input.one">
<button @click="deleteRow(index)">Delete</button>
</li>
</ul>
<select v-model="inputType">
<option selected="selected">Select a field to add</option>
<option value="text">Text</option>
<option value="file">File</option>
<option value="email">email</option>
</select>
<button @click="addRow">Add row</button>
</div>
VueJS
const app = new Vue({
el: '#app',
data: {
inputs: []
},
methods: {
addRow() {
this.inputs.push({
one: ''
})
},
deleteRow(index) {
this.inputs.splice(index,1)
}
}
})
JSFiddle - https://jsfiddle.net/u2r1fpu4/
watch
for changes toinputType
and react to that. That's how you grab what's been selected. On click, simply do the action (add an input) and then set theinputType
tonull
. Also, define it in yourdata()
. – N.B.