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
  • 32,469
  • 5
  • 79
  • 81
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
  • 341,306
  • 83
  • 791
  • 678
1
document.getElementById('element-id').value = 'The Value';
Clive
  • 36,918
  • 8
  • 87
  • 113
0
Value="<script type="text/javascript">document.write(yourVariableName);</script>"
Rachel McMahan
  • 392
  • 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.
  • 21,083
  • 14
  • 84
  • 130