Set a variable in the value of an input

Asked

Viewed 1,659 times

0

I have a variable, for example:

 let exemplo = document.querySelector("#id");

I want to ask the value of this variable and put in the value of a input HTML:

<input type="text" valeu="receberia o valor da variável exemplo" />

How can I do that?

  • Take a look here https://answall.com/questions/37758/colocar-valor-numa-box-of-text-input

  • I looked, but I don’t know how to solve my problem... There’s no way to directly put the value of my variable?

3 answers

1

Without jquery it would be like this:

var texto = 'abc';
var elemento = document.getElementById('iddoinput');
elemento.value = texto

0


Just assign the new value to the attribute value of the object that represents your field. See an example below:

const novoValor = "Stack Overflow em Português";
const campo = document.getElementById("campo");

campo.value = novoValor;
<input id="campo" value="">

0

   var exemplo = document.querySelector("#id");
    
    document.querySelector("[name='txtstart']").value = exemplo;
 
    
    <input type="text" required name="txtstart" style="width:150px" value="">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.