jQuery access input hidden value

JavascriptJquery

Javascript Problem Overview


How can I access <input type="hidden"> tag's value attribute using jQuery?

Javascript Solutions


Solution 1 - Javascript

You can access hidden fields' values with val(), just like you can do on any other input element:

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

alert($('input#foo').val());
alert($('input[name=zyx]').val());
alert($('input[type=hidden]').val());
alert($(':hidden#foo').val());
alert($('input:hidden[name=zyx]').val());

Those all mean the same thing in this example.

Solution 2 - Javascript

Solution 3 - Javascript

There's a jQuery selector for that:

// Get all form fields that are hidden
var hidden_fields = $( this ).find( 'input:hidden' );

// Filter those which have a specific type
hidden_fields.attr( 'text' );

Will give you all hidden input fields and filter by those with a specific type="".

Solution 4 - Javascript

To get value, use:

$.each($('input'),function(i,val){
    if($(this).attr("type")=="hidden"){
        var valueOfHidFiled=$(this).val();
        alert(valueOfHidFiled);
    }
});

or:

var valueOfHidFiled=$('input[type=hidden]').val();
alert(valueOfHidFiled);

To set value, use:

$('input[type=hidden]').attr('value',newValue);

Solution 5 - Javascript

There is nothing special about <input type="hidden">:

$('input[type="hidden"]').val()

Solution 6 - Javascript

If you want to select an individual hidden field, you can select it through the different selectors of jQuery :

<input type="hidden" id="hiddenField" name="hiddenField" class="hiddenField"/> 


$("#hiddenField").val(); //by id
$("[name='hiddenField']").val(); // by name
$(".hiddenField").val(); // by class

Solution 7 - Javascript

If you have an asp.net HiddenField you need to:

To access HiddenField Value:

$('#<%=HF.ClientID%>').val()  // HF = your hiddenfield ID

To set HiddenFieldValue

$('#<%=HF.ClientID%>').val('some value')   // HF = your hiddenfield ID

Solution 8 - Javascript

Watch out if you want to retrieve a boolean value from a hidden field!

For example:

<input type="hidden" id="SomeBoolean" value="False"/>

(An input like this will be rendered by ASP MVC if you use @Html.HiddenFor(m => m.SomeBoolean).)

Then the following will return a string 'False', not a JS boolean!

var notABool = $('#SomeBoolean').val();

If you want to use the boolean for some logic, use the following instead:

var aBool = $('#SomeBoolean').val() === 'True';
if (aBool) { /* ...*/ }

Solution 9 - Javascript

Most universal way is to take value by name. It doesn't matter if its input or select form element type.

var value = $('[name="foo"]');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionI-M-JMView Question on Stackoverflow
Solution 1 - JavascriptTatu UlmanenView Answer on Stackoverflow
Solution 2 - JavascriptAbel ANEIROSView Answer on Stackoverflow
Solution 3 - JavascriptkaiserView Answer on Stackoverflow
Solution 4 - Javascriptthe_rootView Answer on Stackoverflow
Solution 5 - JavascriptMaxim SloykoView Answer on Stackoverflow
Solution 6 - JavascriptRodrigo LongoView Answer on Stackoverflow
Solution 7 - JavascriptBruno FerreiraView Answer on Stackoverflow
Solution 8 - JavascriptGeorg PatscheiderView Answer on Stackoverflow
Solution 9 - JavascriptMarcin ŻurekView Answer on Stackoverflow