-1

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.

1

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

1 Comment

You should avoid using document.write, especially for such a simple application where DOM methods are available - stackoverflow.com/questions/802854/…
0

You can do just:

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

You can also use the jQuery library, it will:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.