Get hidden field value in JavaScript

This article will learn how to get the value of a hidden field using the Javascript programming language. You can use two methods to access the DOM and retrieve an element’s value: a hidden field. The two methods are:

  • document.getElementById(‘elementId’).value: To get the value of an element using its ID.
  • document.getElementsByName(‘elementName’)[0].value:  To get the value of an element by its Name


How to get a hidden field value using JavaScript?

First, let’s add a markup that contains HTML hidden field within its content:

  <asp:HiddenField ID="myHiddenField" runat="server" Value="learmodo" />

Now use the Javascript function GetElementById to access the hidden field and retrieve its value:

function GetValueById() {
    var value;
    value = document.getElementById("<%= myHiddenField.ClientID %>").value;

Or this function to get the hidden field value by name:

function GetValueByName() {
    var value;
    value = document.getElementsByName("myHiddenField")[0].value;

Happy Coding!


Leave a Reply

Your email address will not be published. Required fields are marked *