Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JQuery how can I get the adjacent table cell?

I have a table with multiple rows, and each row contains three cells. Each cell contains a textbox, so it looks like this:

XXXX   XXXX   XXXX

on the keyup event of the first textbox, I want its contents to be copied into the second textbox, but not the third textbox.

my keyup event I can get a reference to the first textbox. Doing .parent() will give me the cell, and if I want, doing .parent() again will give me the row.

What JQuery can I use to get the adjacent textbox?

like image 563
NibblyPig Avatar asked Oct 12 '10 13:10

NibblyPig


People also ask

How to get the table cell value in jQuery?

$('#mytable tr'). each(function() { var customerId = $(this). find("td:first"). html(); });

How to get td from tr in jQuery?

jQuery(". dname"). find("tr td:eq(1)"). val();


1 Answers

You can use .next() to get the next sibling, like this:

var nextTD = $(this).closest("td").next();
//for your case:
$(this).closest("td").next().find("input").val($(this).val());

.parent() works too, .closest() is just a bit more flexible, for you could change your markup and it'd still go to the nearest <td> parent.

like image 143
Nick Craver Avatar answered Sep 30 '22 08:09

Nick Craver