Like normal input fields, values from the hidden input field can be accessed using the element.val().

Following is an example of how to do that.

<input type="hidden" id="xyz" name="zyx" value="foo" />

// Using the ID
$("#xyz").val();

// More faster approach using the ID
$('input#xyz').val();
$('input[name=zyx]').val();

val() function fetches the html value from the specific object and in this case it is the input field. The second approach i.e. $(‘input#xyz’).val() is much more faster than the first one because it uses the ID along with the element tag.

We can also use the name attribute to fetch the value of a hidden variable but it is less efficient because there might be a two elements with same name.

Categorized in:

Tagged in: