0

I am trying to use val() to change the value of a hidden field in asp.net.

My jQuery is as follows:

$("#blue").val("Green");

ASP:

<asp:HiddenField ID="blue" runat="server" clientidmode="static"/>

The result I am looking for is:

<asp:HiddenField ID="blue" runat="server" value="Green" clientidmode="static"/>

Which outputs in html as:

<input type="hidden" value="Green" id="Blue" />

What I am getting:

<input type="hidden" id="Blue" />

I can get $("#blue").text("Green"); to work no problem.

The reason I want to change the value is because I am pulling the value from various json files.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • See the ID "Blue" at the control and "blue" in the Jquery, it's key senstive. I think that you have a mistake copy and paste, and don't use text(). (http://jsfiddle.net/X27Aq/)[Fiddle] – Wilfredo P Apr 26 '14 at 02:14
  • How do you mean? It's not adding value="Green" anyway in the source code? I am using it within an asp:Panel control – aazzaawwaazzaa Apr 26 '14 at 02:16
  • @aazzaawwaazzaa see the Control ID, are dfferents, maybe here is you problem. – Wilfredo P Apr 26 '14 at 02:17
  • @attila he is ussing `clientIdMode=Static` with that dont need to use .ClientID – Wilfredo P Apr 26 '14 at 02:20
  • check `clientidmode` that should be `ClientIdMode` remember the key sensitive. – Wilfredo P Apr 26 '14 at 02:21
  • If the hidden field is created while some DataSourceControl like gridview/datalist/etc data-binds then your code won't work. You'll have to do some [regex matching with id](http://stackoverflow.com/a/1487882/145682) to locate the correct control. Otherwise your code should just do fine. And, the id names are case sensitive too...so `blue` is different from `Blue`. – deostroll Apr 26 '14 at 05:03
  • Hi guys thanks for your suggestions unfortunately nothing is working. Very very strange. – aazzaawwaazzaa Apr 26 '14 at 15:50

1 Answers1

-1

Sorry but I don't understand one thing: When do you need to change that value????

In the example assume that you load a page and then decide to click a button in this case you may change the hiddenfield value with jquery statement or even if you want and you have a complete postback you can change there in button_click event(when it is fired) the value of hiddenfile....May u give more info ????

for the moment i can help you with this few lines of code :

ASP page:

<asp:HiddenField ID="blue" runat="server" clientidmode="static"/>
<asp:button runat="server" id="btnDoSomething" text="Change hidden field value"/>

Possible solution via js with little modification to asp:Button:

<asp:HiddenField runat="server" ID="blue" ClientIDMode="Static" />
<asp:Button runat="server" id="btnDoSomething" text="Change hidden field value" onclientClick="UpdateHiddenField();"/>

  <script>
      function UpdateHiddenField()
      {
          $("#blue").val("Green");
          return false;
      }
  </script>

I checked and it works fine.

madster
  • 23
  • 2
  • 6
makemoney2010
  • 1,222
  • 10
  • 11
  • If you have a new question, please ask it by clicking the [Ask Question](http://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. – khagler Apr 26 '14 at 02:54