Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
How do I set a variable with a number to an html form input value attribute

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda