Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;  }  </script> 

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?

like image 396
KaHeL Avatar asked Oct 14 '11 07:10

KaHeL


People also ask

Can JavaScript read hidden field value?

In JavaScript, you can use following two ways to get hidden field value in a form : document. getElementById('hidden field id'). value.


1 Answers

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.

like image 81
Darin Dimitrov Avatar answered Sep 18 '22 04:09

Darin Dimitrov