views:

35

answers:

3

i have a function to handle click of clickedcell. I want to exctract value of sibling cell with class "desired". how exactly to specify?

 <tr>
     <td class="desired">someValue</td>

     <td><span "clickedcell"></span></td>
     <td></td>

 /tr>
+1  A: 
$("span.clickedcell").click(function() {
  var desired = $(this).closest(".desired").text();
  ...
});

or more simply:

$("span.clickedcell").click(function() {
  var desired = $(this).parent().prev().text();
  ...
});

Also, the selector changes if you want to click anywhere in the cell:

$("td:has(span.clickedcell)").click(function() {
  var desired = $(this).prev().text(); // OR
  var desired2 = $(this).closest(".desired").text();
  ...
});

This can change depending on the nature of the relationship from clickedcell to desired. For example, the second example assumes desired is always the previous sibling. If there can be intervening siblings or it can be after then obviously the traversal needs to change to reflect that.

cletus
thanks, used cal instead of text.
zsharp
I assume you mean val()? If so, that's for input (form) elements.
cletus
+1  A: 
$(".clickedcell").click(function(){
  $(this).closest("td").siblings(".desired").text();
});
Sixten Otto
A: 

Try YUI's DOM collection

YAHOO.util.Dom.getNextSibling(curTD);
Zoidberg