user3319384
user3319384

Reputation: 33

To get the value from particular hidden field

In the below code i have a hidden field.Now i want to get the value from the particular hidden field.I tried the below code i get all the hidden field values.But i want the particular hidden field value.Pls help me to do this. JS:

str = "";
$('input[type=hidden]').each(function (){
   str+=$(this).val() + "$";
  });
if(str != "")
  str = str.substring(0,str.length-1);

alert(str);

asp.net

<asp:hiddenfield ID="hide" runat="server"/>

Upvotes: 0

Views: 76

Answers (4)

mayabelle
mayabelle

Reputation: 10014

Just use the client id of the field you want as the selector:

<asp:hiddenfield ID="hide" ClientID="hide" runat="server"/>

var hiddenFieldValue = $('#hide').val();

Upvotes: 2

Habib
Habib

Reputation: 223187

You can access a particular field based on the ID as shown in other answer. But since your control doesn't specify ClientIDMode you would need:

var hiddenField = $('#' + <%= hide.ClientID %>).val();

Or you can specify ClientIDMode to static (if you are using ASP.Net 4.0 or higher) like:

<asp:hiddenfield ID="hide" runat="server" ClientIDMode="static"/>

and then:

var hiddenField = $('#hide').val();

Upvotes: 2

Matt Burland
Matt Burland

Reputation: 45135

Set the ClientID of your hidden field and then find it on the client side by that id.

<asp:hiddenfield ID="hide" ClientID="myHiddenField" runat="server"/>

Client-side Javascript:

var myHiddenField = $("#myHiddenField");

Or you can set the ClientIDMode to static and then use ID instead.

Upvotes: 0

zoranc
zoranc

Reputation: 2456

why not just use the id selector of the hidden field?

$('#hide').val();

Upvotes: 2

Related Questions