Reputation: 688
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.
var ar_val=[];
for(i=0;i<7;i++)
{
var txtv = $("#<%=PFtxtname"+i+".ClientID%>").val().trim();
ar_val.push(txtv);
}
Upvotes: 1
Views: 1082
Reputation: 62488
You are mixing client side and server side code, it will not work this way.
add a commom css class to textboxes :
<asp:TextBox ID="PFtxtname1" runat="server" CssClass="txtName"></asp:TextBox>
and iterate on elements in Jquery using class selector:
$(".txtName").each(function() {
alert($(this).val()) // for value
alert(this.id) // for id
})
Upvotes: 4
Reputation: 68
you should try this i think its working fine
var ar_val=[];
$("form").find("[id$='PFtxtname']").each(function () {
var txtv = $(this).val().trim();
ar_val.push(txtv);
});
Upvotes: 0