Iterate Through HiddenFields with JQuery

37 views Asked by At

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"/>
1

There are 1 answers

3
Rajaprabhu Aravindasamy On

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" />