0

How do you put a javascript variable into the value="" of an <input> box on a <form>

This must be easy but I do not know.

John Flatness
  • 29,079
  • 5
  • 71
  • 76
Trevor Branch
  • 151
  • 11
  • possible duplicate of [Set the value of a input field with javascript](http://stackoverflow.com/questions/7609130/set-the-value-of-a-input-field-with-javascript) – John Flatness Oct 13 '11 at 22:34

4 Answers4

1

That's not hard. Have a look at the example below:

HTML:

<input id="id_of_your_element">

Javascript:

var yourvariable = "Hello world!";
document.getElementById("id_of_your_element").value = yourvariable;
Rob W
  • 315,396
  • 71
  • 752
  • 644
1
document.getElementById('element-id').value = 'The Value';
Clive
  • 36,705
  • 8
  • 79
  • 108
0
Value="<script type="text/javascript">document.write(yourVariableName);</script>"
Rachel McMahan
  • 382
  • 1
  • 8
  • You should avoid using document.write, especially for such a simple application where DOM methods are available - http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – nickb Oct 13 '11 at 22:30
0

You can do just:

document.getElementById('inputId').value = yourVariable;

You can also use the jQuery library, it will:

$('#inputId').val(yourVariable);
GG.
  • 17,726
  • 11
  • 69
  • 117