KaHeL KaHeL - 5 months ago 45
HTML Question

Pass a javascript variable value into input type hidden value

I would like to assign value of product of two integer numbers into a hidden field already in the html document.
I was thinking about getting the value of a javascript variable and then passing it on a input type hidden.
I'm having a hard time to explain but this is how it should work:

Script Example

<script type="text/javascript">
function product(a,b){
return a*b;

above computes the product and i want the product to be in hidden field.

<input type="hidden" value="[return value from product function]">

How is this possible?


You could give your hidden field an id:

<input type="hidden" id="myField" value="" />

and then when you want to assign its value:

document.getElementById('myField').value = product(2, 3);

Make sure that you are performing this assignment after the DOM has been fully loaded, for example in the window.load event.