Click here to Skip to main content
15,847,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can I access the similar asp control id's by using for loop? I have the following asp text boxes.

<asp:TextBox ID="PFtxtname1" runat="server"></asp:TextBox>
<asp:TextBox ID="PFtxtname2" runat="server"></asp:TextBox>
<asp:TextBox ID="PFtxtname3" runat="server"></asp:TextBox>
<asp:TextBox ID="PFtxtname4" runat="server"></asp:TextBox>
<asp:TextBox ID="PFtxtname5" runat="server"></asp:TextBox>
<asp:TextBox ID="PFtxtname6" runat="server"></asp:TextBox>


and I want to access all these text box value through for loop using jQuery, how can I do?

I am tried to get the value by below code, but it shows error.

JavaScript
var ar_val=[];
for(i=0;i<7;i++)
{
  var txtv = $("#<%=PFtxtname"+i+".ClientID%>" + i).val().trim();
  ar_val.push(txtv);
}
Posted

1 solution

That code won't work as the server code is executed and the results sent to the browser to display, so you can't mix server code and client code. You'll need to do something like this;

XML
<asp:TextBox ID="PFtxtname1" runat="server" Text="a"></asp:TextBox>
<asp:TextBox ID="PFtxtname2" runat="server" Text="b"></asp:TextBox>
<asp:TextBox ID="PFtxtname3" runat="server" Text="c"></asp:TextBox>
<asp:TextBox ID="PFtxtname4" runat="server" Text="d"></asp:TextBox>
<asp:TextBox ID="PFtxtname5" runat="server" Text="e"></asp:TextBox>
<asp:TextBox ID="PFtxtname6" runat="server" Text="f"></asp:TextBox>

<script type="text/javascript">
    $(document).ready(function () {
        $("input[id*='PFtxtname']").each(function () {
            alert($(this).val());
        });
    });
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900