How to get hidden field value using JavaScript

Tutorials PanelPosted by

Here are the two ways to get a hidden filed value using JavaScript:

  1. document.getElementById(‘Id of the hidden fieled).value
  2. document.formName.elements[‘name of the hidden field].value

You can use the way you prefer; both will return the same value.

And here is an example:

JavaScript:

<script type="text/javascript">
function GetHiddenFieldValue(){
var HiddenFiledID;
var HiddenFiledName;
HiddenFiledID = document.getElementById(hfID).value; 
HiddenFiledName = document.formName.elements[hfName].value;
   alert(HiddenFiledID);
   alert(HiddenFiledName);

}
</script>

HTML:

<h1> Get hidden field value using JavaScript </h1>
<form name="frm">
	<input type=hidden id=" hfID " name=" hfName "
                  value="Tutorials Panel Rocks!"/>

	<input type="button" id="btn" value="Get Value" onclick=" GetHiddenFieldValue()" />
</form>

 

Leave a Reply