How to access text from neighboring table cell?
I have a set of search results presented in a table. Each row has a radio box. Once the user has selected a row I would like to access the description text from the neighboring cell.
Using jQuery or straight javascript, what is the best way to do this?
<tr class="odd">
<td class="chosenCode"><input type="radio" value="12开发者_JAVA百科3" name="chosenCode"></td>
<td class="codeFound">123</td>
<td class="descriptionFound">This is description text for code 123</td>
</tr>
Thanks
$("table input:radio").change(function () {
alert( $(this).closest("tr").children(".descriptionFound").text() );
});
Or, more elaborate:
// prepare once
$("table input:radio").each(function () {
var descr = $(this).closest("tr").children(".descriptionFound").text();
$(this).data("descr", descr);
});
// use
$("table input:radio").change(function () {
alert( $(this).data("descr") );
});
Inside the event callback function you can use this code to get the content of the description element.
$(this).next('.descriptionFound').text();
精彩评论