user7184660
user7184660

Reputation:

How to fill in input with a var value?

How do I fill in input with a var value?

For example: number= 10, user clicks button, input auto 'types' number 10.

var number = 10;
<input type="number">

<button onclick="value()">Fill in value</button>

Upvotes: 0

Views: 226

Answers (1)

Suren Srapyan
Suren Srapyan

Reputation: 68645

Get the element and set it's value. Use document.getElementById() to get the input element and use value property to set it's value;

var number = 10;

var txt = document.getElementById('txt');

function setValue(){
  txt.value = number;
}
<input id="txt" type="number">

<button onclick="setValue()">Fill in value</button>

Upvotes: 1

Related Questions