jQuery access input hidden value

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


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.


The most efficient way is by ID.

$("#foo").val(); //by id

You can read more here:

https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Writing_efficient_CSS

https://developers.google.com/speed/docs/best-practices/rendering?hl=it#UseEfficientCSSSelectors