[jQuery] Re: Get the td below and above

2009-11-18 Thread Paul Mills
Hi,
Assuming your table has the same number of tds in each tr.
You can try this approach:
  First find the position of the selected td in its tr.
  Then find the prev() or next() tr.
  Then select the td with the same position in that tr.

Try this:
$('td').click(function(){
  var trparent = $(this).parent('tr');
  var position = $('td', $(this).parent('tr')).index(this);
  $('td:eq('+ position +')', $(trparent).prev('tr')).css
('color','white');
  $('td:eq('+ position +')', $(trparent).next('tr')).css
('color','green');
});

Paul

On Nov 18, 11:13 am, Jan jha...@gmail.com wrote:
 Hi there,

 I'm new to jQuery and was looking for a way to get a table cell above
 or below the current one.
 What I mean is a function like .prev() or .next() but those only help
 me getting the cell to the left or to the right.


[jQuery] Re: Get the td below and above

2009-11-18 Thread Dave Methvin
If the table is really big it might be better to use event delegation:

$('table').click(function(e){
  var $td = $(e.target).closest('td'),
  $tr = $td.parent(),
  pos = $tr.children().index($td),
  $up = $tr.prev().children().eq(pos),
  $dn = $tr.next().children().eq(pos);
  $up.css(background-color, red);
  $dn.css(background-color, blue);
});


[jQuery] Re: Get the td below and above

2009-11-18 Thread Jan
Thank you both, it works :)