最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get row of element in cell by javascriptjquery? - Stack Overflow

programmeradmin2浏览0评论

I have a simple table like this

<table id="tempTable">
    <tbody>
        <tr id="row_01">
            <td>
                <button onclick="btnclick(this);" >Save Row</button>
            </td>
        </tr>
        <tr id="row_02">
            <td>
                <button onclick="btnclick(this);" >Save Row</button>
            </td>
        </tr>
    </tbody>
</table>

function btnclick(e) {
    var currentRow = $(e).parent().parent();
    alert(currentRow.id);
}

I want to determine which row where the button clicked was placed. So I use some jquery method in btnclick() as you see abow. But sometime I don't know how deep level the button was placed in row (), so Im looking for a way to get an ancestor by tag <tr> of a element.

Anybody help me, thanks?

I have a simple table like this

<table id="tempTable">
    <tbody>
        <tr id="row_01">
            <td>
                <button onclick="btnclick(this);" >Save Row</button>
            </td>
        </tr>
        <tr id="row_02">
            <td>
                <button onclick="btnclick(this);" >Save Row</button>
            </td>
        </tr>
    </tbody>
</table>

function btnclick(e) {
    var currentRow = $(e).parent().parent();
    alert(currentRow.id);
}

I want to determine which row where the button clicked was placed. So I use some jquery method in btnclick() as you see abow. But sometime I don't know how deep level the button was placed in row (), so Im looking for a way to get an ancestor by tag <tr> of a element.

Anybody help me, thanks?

Share Improve this question edited Mar 18, 2012 at 11:22 StevenChow asked Mar 18, 2012 at 11:17 StevenChowStevenChow 1551 gold badge3 silver badges10 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12

Try this :

function btnclick(e) {
    var currentRow = $(e).closest('tr');
    alert(currentRow.id);
}

The closest() function will return the closest ancestor referenced by its selector. In this case the selector is simple a <tr> element.

Taken from the jQuery docs :

.closest( selector )

Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.


A better method could be used if you change your HTML a little bit. If you placed the same class on each of your buttons.
Eg :

<td>
  <button class="myBtnClass" >Save Row</button>
</td>

Then your jQuery would look like this :

$(".myBtnClass").on('click',function(){
    var currentRow = $(this).closest('tr');
    alert(currentRow.attr('id'));
});

This function will capture a click on any element with the .myBtnClass class.

The jQuery way is:

$('#tempTable').find('button').click(function() {
    var currentRow = $(this).closest('tr');
});

how about using closest

function btnclick(e) {
    var currentRow = $(e).closest('tr');
    alert(currentRow.id);
}
发布评论

评论列表(0)

  1. 暂无评论