Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Value of an asp:HiddenField using jQuery

I have two pages. From the first page, I open a modal with a querystring that holds that value of a client name. I then use this to set a hiddenfield on the modal that opened.

I need a TextBox on the new modal to display the value that has been sent through from the first screen.

I've tried getting the value using:

var hv = $('hidClientField').val();` 

But this doesn't seem to work.

This is my hidden field:

<asp:HiddenField ID="hidClientName" runat="server" />` 

I set it in the code behind on the Page_Load like this:

hidClientName.Value = Request.QueryString["Client_Name"] ?? "";` 

Any ideas will be much appreciated.

like image 363
Melanie Avatar asked Jan 18 '12 10:01

Melanie


People also ask

How to get value from hidden input field in jQuery?

#1 jQuery Code To Get hidden field value by IDvar getValue= $("#myInputHidden"). val(); alert(getValue); Here we select the input hidden field by the ID, i.e myInputHidden, and with the jQuery . val() method we get the value of it.

Can we use jQuery in ASP NET?

To begin with using jQuery with ASP.NET, first download the latest version the jQuery library from jQuery website and unzip or copy the file in your project or Visual studio solution. Microsoft Visual studio 2010 and 2012 include jQuery by default and provide intellisense to use jQuery.


2 Answers

Try any of the following

  1. If ASP.Net control and javascript both are on same page, then use

    var hv = $("#"+ '<%= hidClientField.ClientID %>').val(); 
  2. If you want to access the control from some JS file, then

    // 'id$' will cause jQuery to search control whose ID ends with 'hidClientField' var hv = $('input[id$=hidClientField]').val(); 
  3. You can use class name selector to achieve same. Check out this similar question.

In asp.net, controls id is mangled. Because of this your code is not working.

Hope this works for you.

like image 154
Amar Palsapure Avatar answered Oct 14 '22 23:10

Amar Palsapure


You forgot the # in your selector to select by ID:

var hv = $('#hidClientField').val(); 

Although asp.net generates ID based on the naming containers so you might end up with an ID like ctl1$hidClientField. You can then use the "attribute ends with" selector:

var hv = $('input[id$=hidClientField]').val(); 

Check the documentation about jQuery selectors

like image 40
Didier Ghys Avatar answered Oct 14 '22 23:10

Didier Ghys