开发者

Getting the attribute of a parent

I'm trying to get the id of a table using a table row. While I am in a loop:

$(this).parent.attr('id');

However I am getting an error. Is my syntax right?

Thanks.

EDIT:

<table id="21-rawTable" border="1"><tbody><tr class="even notSelected eClr" name="0" id="beaf-32;Developmental-Stage_Embryo-0-12h;ChIP-chip;Rep-1;input;Dmel_r54;modENCODE_21;BEAF_Input_0.CEL" title="/modencode/modencode-dcc/symbolic_links/Dmel_r5.4/Non-TF-Chromatin-binding-factor/ChIP-chip/raw-arrayfile_CEL/beaf-32/Embryo 0-12h/" style="cursor:pointer;" "="" onclick="selected('beaf-32;Developmental-Stage_Embryo-0-12h;ChIP-chip;Rep-1;input;Dmel_r54;modENCODE_21;BEAF_Input_0.CEL','[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]')"><td class="checkbox">开发者_如何学运维;<input type="checkbox" id="beaf-32;Developmental-Stage_Embryo-0-12h;ChIP-chip;Rep-1;input;Dmel_r54;modENCODE_21;BEAF_Input_0.CELcheckBox" checked=""></td><td>beaf-32;Developmental-Stage_Embryo-0-12h;ChIP-chip;Rep-1;input;Dmel_r54;modENCODE_21;BEAF_Input_0.CEL</td><td>0 bytes</td></tr><!-- table--></tbody></table>

Using this code to get the id of the table which in this case is "21-rawTable"

$(".selected").each(function () {
var parentname = $(this).parent().attr('id');
        alert (parentname);
});

I resolved the issue. I think it was giving me the direct parent. Instead I had to do a search for the closest ancestor like such:

var parentname = ($(this).closest('table')).attr('id');

Thanks.


parent[API Ref] is a method, so you should call it like this:

$(this).parent().attr('id');


I think you need to include the parentheses on the call to the parent() function.


I don't see any tags with class='selected' in your sample HTML. However:

  • the parent of the checkbox is the TD.
  • the parent of the TD is the TR.
  • the parent of the TR is the TBODY.
  • the parent of the TBODY is the TABLE.

You can use .closest(selector) to get the table ancestor from anywhere inside of it, though:

$(this).closest('table').attr('id');

http://api.jquery.com/closest/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜