Skip to content

Instantly share code, notes, and snippets.

@donamkhanh
Created May 17, 2012 17:22
Show Gist options
  • Save donamkhanh/2720326 to your computer and use it in GitHub Desktop.
Save donamkhanh/2720326 to your computer and use it in GitHub Desktop.
Get root parent of element by row id. Root parent is TR has class = ".parent" and depth = 0
<html>
<head>
<title>Test parent</title>
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
var a = '';
var i = 0;
while(i >= 0) {
a += '.parent()';
var b = "$('tr#6')" + a;
b = eval(b);
console.log(i + ' = ' + b.attr('depth'));
if (b.attr('depth') == 0) {
console.log(b.get(0).id);
b.css('background', '#000');
break;
}
i++;
}
});
</script>
<style>
table td {border: solid 1px;}
</style>
</head>
<body>
<table border="1" cellspacing="2">
<tbody>
<tr class="parent" depth="0" id="1">
<td>
Hello
</td>
<td>
World
</td>
</tr>
<tr class="parent" depth="0" id="2">
<td colspan="2">
<table border="1" cellspacing="2">
<tbody>
<tr class="child" id="3">
<td>
Goodbye
</td>
<td>
World
</td>
</tr>
<tr class="parent" depth="1" id="4">
<td colspan="2">
<table border="1" cellspacing="2">
<tbody>
<tr class="child" id="5">
<td>
Goodbye
</td>
<td>
World
</td>
</tr>
<tr class="child" id="6">
<td>
Goodbye
</td>
<td>
World
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment