admin管理员组文章数量:1287577
I have a dynamically created table as:
<div id="gameListDiv">
<table id="gameListTable">
<tr id="1">
<td>A</td>
<td>B</td>
<td class="tpButton">C</td>
</tr>
<tr id="2">
<td>D</td>
<td>E</td>
<td class="tpButton">F</td>
</tr>
</table>
</div>
I have a listener:
$('#gameListDiv').on('click','.tpButton', function(toggleThisTP) {
// If user clicks C, return the row ID of '1'
// if user clicks F, return the row ID Of '2'
});
As in the code ments just above, how to I get the value if the ID tag of the specific row when the user clicks a cell in the 3rd column of the table?
I have a dynamically created table as:
<div id="gameListDiv">
<table id="gameListTable">
<tr id="1">
<td>A</td>
<td>B</td>
<td class="tpButton">C</td>
</tr>
<tr id="2">
<td>D</td>
<td>E</td>
<td class="tpButton">F</td>
</tr>
</table>
</div>
I have a listener:
$('#gameListDiv').on('click','.tpButton', function(toggleThisTP) {
// If user clicks C, return the row ID of '1'
// if user clicks F, return the row ID Of '2'
});
As in the code ments just above, how to I get the value if the ID tag of the specific row when the user clicks a cell in the 3rd column of the table?
Share Improve this question asked Jun 11, 2015 at 14:18 Dan ADan A 1032 silver badges10 bronze badges3 Answers
Reset to default 8This should work:
$('#gameListDiv').on('click','.tpButton', function() {
var id = jQuery(this).closest('tr').attr('id');
alert(id);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="gameListDiv">
<table id="gameListTable">
<tr id="1">
<td>A</td>
<td>B</td>
<td class="tpButton">C</td>
</tr>
<tr id="2">
<td>D</td>
<td>E</td>
<td class="tpButton">F</td>
</tr>
</table>
</div>
Link to closest() documentation
Use jQuery closest() and attr()
$('#gameListDiv').on('click','.tpButton', function(toggleThisTP) {
var row = $(this).closest('tr');
var id = row.attr('id');
});
Use Class
<div id="gameListDiv">
<table id="gameListTable">
<tr class="gamelist" id="1">
<td>A</td>
<td>B</td>
<td class="tpButton">C</td>
</tr>
<tr class="gamelist" id="2">
<td>D</td>
<td>E</td>
<td class="tpButton">F</td>
</tr>
</table>
</div>
Jquery
$(".gamelist").click(function(){
var id = $(this).attr('id');
alert(id);
});
本文标签: javascriptHow to get the Value of an ID from a TR of a Clicked TD ElementStack Overflow
版权声明:本文标题:javascript - How to get the Value of an ID from a TR of a Clicked TD Element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741308805a2371529.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论