Reputation: 31
I want to get the <tr>
element of a table by knowing the ID of one of it's elements (div).
Here's what the table looks like in HTML:
<table id="table-0">
<thead>
<tr role="row">
<th>house</th>
<th>phone</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td class="">
<div id="56"></div>my house
</td>
<td class="">
<div id="57"></div>1234
</td>
</tr>
</tbody>
</table>
So I would like to get the <tr>
element that has a <td>
element that contains a <div>
with the ID 56 (in this example).
Here's what I tried in jQuery (data['id'] = 56):
var tr = $("tr:has(td:has($(\"#\" + data['id']))))");
Upvotes: 0
Views: 21548
Reputation: 3295
First: is totally invalid to use same ID to more of one element because the ID is Unique. Its a HTML Standard.
But if you want to get the selector for your element you can use this (without needing ID):
$('tr.odd').children().children();
This is the correct way because you dont need a Duplicated ID.
Upvotes: 0
Reputation: 2587
You can get your output with below jQuery expression
$('tr').find('#56').closest('tr')
And also remember ID assigned to HTML
controls should unique throughout page.
With above expression you will get tr
object (i.e. whole tr with controls of id specified)
Upvotes: 0
Reputation: 2375
You can also find the tr by element Id :
var tr = $('#Id').closest("tr");
In your case :
var tr = $('#56').closest("tr");
Upvotes: 4
Reputation: 145478
var tr = $("tr:has('#" + data['id'] + "')");
N.B.: Check that all your elements in a single page have unique IDs.
Upvotes: 1