I'm trying to set a numeric value to an html form's input value attribute as a variable. I'm aware that the value attribute implicitly converts to a string. How can I get the variable to be read before it is converted to a string?
<input type="hidden" name="amount" value="" id="amount">
This is the input whose value I want to set.
document.getElementById("amount").setAttribute('value', this.amount)
This is one method I've been trying.
I'm aware that the value attribute implicitly converts to a string.
It's not correct, It will return number only if you are assigning numeric value into a :value attribute.
Demo :
new Vue({
el: '#app',
data: {
productName: 'xyz',
amount: 10
},
methods: {
submitForm() {
console.log(this.productName, typeof this.productName)
console.log(this.amount, typeof this.amount)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<input type="text" id="productName" name="productName" :value="productName">
<input type="hidden" id="amount" name="amount" :value="amount">
<button @click="submitForm">Submit</button>
</div>
You can see the return data type in the console on click of submit.