views:

95

answers:

2

How can I get the target from the with td rowspan=4

Not sure about:

$("#fromTd").parent("tr").next().eq($("#fromTd").attr("rowspan")-1)


<table>
   <tr>...</tr>
   <tr>...</tr>
   <tr><td id="fromTd" rowspan="4"></td></tr>
   <tr>...</tr>
   <tr>...</tr>
   <tr>...</tr>
   <tr>...</tr> -> target (can be aleatory position..)
</table>
+1  A: 

If you're trying to do what that line looks like it's trying to do—selecting the row after the end of the current cell's rowspan—you would just need nextAll() instead of next(), which only ever returns the immediate next element sibling.

var td= $('#fromTd');
var nextr= td.parent().nextAll().eq(td.attr('rowspan')-1);

Alternatively if you've got lots of following rows and you don't want to have to select them all to pick a single one out, you could do it slightly more efficiently with the standard DOM rows and rowIndex properties:

var nextr= td.closest('table')[0].rows[td[0].parentNode.rowIndex+td[0].rowSpan];
bobince
+1  A: 

Correct me if I am wrong but it sounds like what you want is to get the next <tr> after X rows where X is the rowspan of a given <td> - 1. In other words, you want the next row into which that <td> will NOT extend.

If that is the case, this should do the trick:

var eq = $("#fromTd").attr("rowspan") - 1;
var row = $("#fromTd").parent("tr").nextAll(':eq(' + eq + ')');

Here's a live demo: http://jsfiddle.net/wLPA9/

Ender
Just a matter of preference, but I find this slightly more readable: `$("#fromTd").parent("tr").nextAll().eq(eq);`
Peter Ajtai
It definitely is more readable :) I'm not sure if it offers any tangible benefits, but I was going for using fewer selectors. Like you said, just a matter of preference.
Ender
`:eq(n)` is executed as `.eq(n)` in the newest jQuery version (or will be in >1.4.2) [according to John Resig](http://github.com/jquery/jquery/commit/f1f6bc3ec43d86f5b2a0c2b8bf711a2d35930717#commitcomment-157856). So you might as well save jQuery a step and avoid the `":eq(" + n + ")"` string concatenation and verbosity by using `.eq(n)`.
David Murdoch
Cool, thanks for the tip :)
Ender