2

How can I iterate through each of these hidden fields and change each one's value with JQuery?

    <asp:HiddenField ID="field1" runat="server"/>
    <asp:HiddenField ID="field2" runat="server"/>
    <asp:HiddenField ID="field3" runat="server"/>
John Smith
  • 33
  • 1
  • 3

1 Answers1

3

You can do it by combining :hidden selector and attribute starts with selector,

$(":hidden[id^=field]").val(function(_,val){
 return "anyValueYouWant";
});

DEMO

To make sure that the ID not change use also the ClientIDMode="Static" on each control. The ID can change if you place that code on any page that use any master page, or is inside any custom control. So your control must be:

<asp:HiddenField ID="field1" runat="server" ClientIDMode="Static" />
<asp:HiddenField ID="field2" runat="server" ClientIDMode="Static" />
<asp:HiddenField ID="field3" runat="server" ClientIDMode="Static" />
Aristos
  • 66,005
  • 16
  • 114
  • 150
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130