Currently, I have a VueJS app where you can click a button to create a set of two inputs. These two inputs perform a simple math function on key up. These math functions work with one set of inputs. However, since I'm using document.getElementById to grab the inputs, it will only work on the first set of inputs. Is there any way I can uniquely identify each set of inputs so I can perform this math function individually?
Example:
Vue.component('product', {
template: `
<div>
<select>
<option>Product One</option>
<option>Product Two</option>
</select>
<input class="input" id="bags" @keyup="$emit('calculatevolume')" type="number" placeholder="Bags">
<input class="input" id="volume" @keyup="$emit('calculatebags')" type="number" placeholder="Volume">
<button @click="$emit('remove')">Delete</button>
</div>
`
})
new Vue({
el: '#app',
data: {
index: 0,
products: []
},
methods: {
addProduct: function() {
this.products.push(this.index);
this.index++;
},
calculatevolume: function() {
var inputs = document.querySelectorAll(".input");
var bags = document.getElementById("bags");
var volume = document.getElementById("volume");
volume.value = bags.value * 25;
},
calculatebags: function() {
var inputs = document.querySelectorAll(".input");
var bags = document.getElementById("bags");
var volume = document.getElementById("volume");
bags.value = volume.value / 25;
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.6/vue.min.js"></script>
<div id="app">
<button
v-model="index"
v-on:click="addProduct"
placeholder="Add Product">Add Product</button>
<div class="products">
<div
is="product"
v-for="(product, index) in products"
:id="index"
:key="product"
@remove="products.splice(index, 1)"
@calculatevolume="calculatevolume"
@calculatebags="calculatebags">
</div>
</div>
</div>