Trevor Branch
Trevor Branch

Reputation: 149

HTML and javascript variables

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.

Upvotes: -1

Views: 104

Answers (4)

GG.
GG.

Reputation: 21844

You can do just:

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

You can also use the jQuery library, it will:

$('#inputId').val(yourVariable);

Upvotes: 0

Rachel McMahan
Rachel McMahan

Reputation: 392

Value="<script type="text/javascript">document.write(yourVariableName);</script>"

Upvotes: 0

Clive
Clive

Reputation: 36956

document.getElementById('element-id').value = 'The Value';

Upvotes: 1

Rob W
Rob W

Reputation: 349002

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;

Upvotes: 1

Related Questions