I've spent the entire day searching, but I still haven't found a solution to this issue.
I'm working on an app where I need to use v-model for reactive input that displays its value in another div element. What I really want is to be able to insert this input with v-model dynamically anywhere in my HTML code.
Here's a snippet of my mini app, where everything works fine except for v-model not functioning properly due to dynamic insertion.
Does anyone know how to fix this?
Check out the code below:
<template>
<div>
<div id="content"></div>
<button type="button" @click="createNew('typeOne')">Create Type One</button>
<button type="button" @click="createNew('typeTwo')">Create Type Two</button>
<h3 style="color:#000000;" id="formTypeOne0">formTypeOne0</h3>
<h3 style="color:#000000;" id="formTypeOne1">formTypeOne1</h3>
<h3 style="color:#000000;" id="formTypeTwo0">formTypeTwo0</h3>
<h3 style="color:#000000;" id="formTypeTwo1">formTypeTwo1</h3>
</div>
</template>
<script>
export default {
data () {
return {
formTypeOne0: '',
formTypeOne1: '',
formTypeTwo0: '',
formTypeTwo1: '',
numberTypeOne: 0,
numberTypeTwo: 0
}
},
methods:{
createNew(type) {
if (type === 'typeOne') {
var html = '<input type="text" :value="formTypeOne' + this.numberTypeOne + '" v-model="formTypeOne' + this.numberTypeOne + '">'
document.getElementById('content').insertAdjacentHTML('beforeend', html)
this.numberTypeOne++
} else if (type === 'typeTwo') {
var html2 = '<input type="text" :value="formTypeTwo' + this.numberTypeTwo + '" v-model="formTypeTwo' + this.numberTypeTwo + '">'
document.getElementById('content').insertAdjacentHTML('beforeend', html2)
this.numberTypeTwo++
}
}
},
watch: {
formTypeOne0: function(val, oldVal) {
document.getElementById('formTypeOne0').innerHTML = val
},
formTypeOne1: function(val, oldVal) {
document.getElementById('formTypeOne1').innerHTML = val
},
formTypeTwo0: function(val, oldVal) {
document.getElementById('formTypeTwo0').innerHTML = val
},
formTypeTwo1: function(val, oldVal) {
document.getElementById('formTypeTwo1').innerHTML = val
}
}
}