I have a table where each row has a CSS id like this:

In a row, a user can click on a particular element within a cell. I want to find the row that has been clicked on.

So for example:

......

In this case, I want to respond when the user clicks on a class='anotherclass' element. I want to get the id of the row containing this clicked element but I don't want to use $(this).parent().parent().parent().... etc. because there are multiple levels of parentage and it gets ugly.

Is there a way to get the row containing this element with using .parent() multiple times?

UPDATE: Thanks everyone. That is perfect.

解决方案$('.anotherclass').click(function () {

alert($(this).closest('tr').prop('id'));

});

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐