Get text value of span in repeater by using javascript or jquery

Question!

i can't get value of span (or label) here is my html code

<asp:Repeater ID="rpt_district" runat="server" >
            <ItemTemplate>
            <tbody id='bodies'>

                <tr>
                <td>
                    <span id="district_id"><%# Eval("DISTRICT_ID") %></span>
                </td>
                <td><%# Eval("DISTRICT_NAME") %></td>
                <td>
                    <asp:Label ID="lb_city_id" runat="server" Text='<%# Eval("CITY_ID") %>'></asp:Label>
                </td>
                <td style="float:left; margin-left:5px;">
                    <asp:CheckBox ID="cb_status" runat="server" onclick="javascript: return false;" />
                    <asp:HiddenField ID="hf_status" Value='<%# Eval("ENABLE") %>' runat="server" />
                </td>
                <td>
                    <div style="float:left; margin-left:4px;" >
                        <asp:ImageButton ID="ib_edit" runat="server" ImageUrl="~/Hinh/icons/Edit.png" ToolTip="Edit" />
                    </div>
                    <div style="float:left; margin-left:4px;">
                        <asp:ImageButton ID="ib_delete" runat="server" ImageUrl="~/Hinh/icons/Trash.png" 
                                        ToolTip="Delete" CommandArgument='<%# Eval("DISTRICT_ID") %>'
                                        OnClientClick="getvalue(); return false;"/>

                    </div>
                </td>
                </tr>


            </tbody>
            </ItemTemplate>
            </asp:Repeater>

i want to get value of district_id in span tag (or label is ok) by using javascript when clicking on imagebutton

function getvalue() {
    var thisspan = $('.district_id').eq(0);
    alert(thisspan);
}

but it just get the value in first column be cause of ea(0) i'm just want to get value by using javascript or jquery not code behind thanks.....



Answers

You are searching after a class but you have an id

var thisspan = $('#district_id').eq(0);



. is the selector for classes and # is selector for ids. Your html has the id not the class, so your code should be like:

var thisspan = $('#district_id').eq(0);

Also, id's are unique in html so you do not need the .eq(0):

var thisspan = $('#district_id');

Finally, to get the text value inside of the span tah you need to do:

var thisspan = $('#district_id').html();

or

var thisspan = $('#district_id').text();


Add a css class to your image button like this. No two elements in the DOM should have the same ID.

By : Krishna


This video can help you solving your question :)
By: admin