Zaki Zaki - 1 month ago 13
jQuery Question

how to get label text inside repeater

I am trying to get the text of a lable when someone clicks on a link button:

this is my code :

$(document).ready(function () {

$('[id*="lnkbtn_Remove"]').click(function (event) {
event.preventDefault();

var id = $("span[id*=lbl_Reference]").text();
alert(id);
});
});


But this gets me all the reference in the reapter as I am using "*". Is there anyway to get the actual reference on the row it has been clicked. Here is my html:

<asp:Repeater ID="rpt" runat="server" >
<ItemTemplate>
<div class="test">
<table>
<tr>
<td>
<div style="width: 230px;">
<asp:Label ID="lbl_Len" runat="server" CssClass="font_bold"></asp:Label>
<br />
<asp:Label ID="lbl_Reference" runat="server"></asp:Label>
</div>
</td>
<td align="left">
<div style="width: 80px;">
<asp:LinkButton ID="lnkbtn_Remove" runat="server" CssClass="rpt_item_Remove">Remove</asp:LinkButton>
</div>
</td>

</tr>
</table>
</div>
</ItemTemplate>
</asp:Repeater>

Answer

You can get it by something like this,

 var lablelId = $(this).closest('.test').find('[id*=lbl_Reference']).attr('id');
 var lablelText = $(this).closest('.test').find('[id*=lbl_Reference]').text();