简体   繁体   中英

How to find the id of an element in an html table using jquery

I need to find the id of an image button (within table cell 1) on clicking another image button (within table cell 2).

I cannot hardcode any index in my javascript / jquery.

Is there any other way to fetch the id of the second button within that particular row of a table?

Please help

Code Sample

<tr class="home-history-grid-row">
    <td>
        <img id="test" height="16px" width="16px" onclick="changeImage(this)" src="SiteImages/deleteconfirm.png" title="Confirm Delete" alt="Delete">
    </td>
    <td>
        <input id="ctl00_Content_NotificationHistory_ctl02_ImgDelete" type="image" style="height: 16px; width: 16px; border-width: 0px;" src="SiteImages/X.gif" clientidmode="Static" disabled="disabled" name="ctl00$Content$NotificationHistory$ctl02$ImgDelete">
    </td>
</tr>
function changeImage(me){
  alert(me.id)
}

If I understand you correctly you need to find the id of the input from clicking the image? If so, then this would do it for you:

function changeImage(self) {
    $(self).closest('tr').find('td input').attr('id');
}

The following code must solve your problem:

function changeImage(elm) {
    var otherImageId = $(elm).parent().next().find("input:first").attr("id");

    // ... use otherImageId here ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM