I have the following HTML:
<table id="MwDataList" class="data" width="100%" cellspacing="10px">
....
<td class="centerText" style="height: 56px;">
<input id="selectRadioButton" type="radio" name="selectRadioGroup">
</td>
....
</table>
In other words I have a table with few rows, in each row in the last cell I have a radio button.
How can I get parent row for selected radio button?
What I have tried:
function getSelectedRowGuid() {
var row = $("#MwDataList > input:radio[@name=selectRadioGroup]:checked :parent tr");
var guid = GetRowGuid(row);
return guid;
}
But it seems like this selector is incorrect.
Answer
How to find the closest row?
Using
.closest()
:Using
.parent()
:Check this
.parent()
method. This is alternative of a.prev()
and.next()
.Get All Table Cell
<td>
VIEW DEMO
Get Only Specific
<td>
VIEW DEMO
Useful methods
.closest()
- get the first element that matches the selector.parent()
- get the parent of each element in the current set of matched elements.parents()
- get the ancestors of each element in the current set of matched elements.children()
- get the children of each element in the set of matched elements.siblings()
- get the siblings of each element in the set of matched elements.find()
- get the descendants of each element in the current set of matched elements.next()
- get the immediately following sibling of each element in the set of matched elements.prev()
- get the immediately preceding sibling of each element in the set of matched elementsTry this.
You don't need to prefix attribute name by
@
in jQuery selector. Useclosest()
method to get the closest parent element matching the selector.You can simplify your method like this
closest()
- Gets the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.As a side note, the ids of the elements should be unique on the page so try to avoid having same ids for radio buttons which I can see in your markup. If you are not going to use the ids then just remove it from the markup.