views:

32

answers:

2

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="123" name="chosenCode"></td>
 <td class="codeFound">123</td>
 <td class="descriptionFound">This is description text for code 123</td>
</tr>

Thanks

+2  A: 
$("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") );
});
Tomalak
Great just what I was looking for, thank you!
Chris
+1  A: 

Inside the event callback function you can use this code to get the content of the description element.

$(this).next('.descriptionFound').text();
Nithesh Chandra
Great just what I was looking for, thank you!
Chris