Hassan Ali Shahzad
Hassan Ali Shahzad

Reputation: 2724

Get java script variable in HTML

I want to get variable value in hidden HTML field. My code like:

<script lang=”text/javascript”>
   Var counter;
   Loop{
     Some Code . . . . 
     Counter++;
   }//end loop
</script>

<html>
   <body>
      // some code 
      <input type “hidden” name=”total” id=”total” value=”here I want to get Counter”>
   </body>
</html>

Upvotes: 0

Views: 3241

Answers (2)

ComFreek
ComFreek

Reputation: 29424

1.) Get the DOM object of the element

var elem = document.getElementById("total");

2.) Set the value

elem.value = your_variable;

Example:

var Counter = 0;
var elem = document.getElementById("total"),
while(condition == true) {
    // some code
    Counter++;
    elem.value = Counter;
}

Upvotes: 2

Thein Hla Maw
Thein Hla Maw

Reputation: 683

You can get the value of form field by Javascript DOM API.

<script lang=”text/javascript”>
var total = document.getElementById("total").value;

Var counter;
Loop{
Some Code . . . . 
Counter++;
}//end loop

Upvotes: 1

Related Questions