user2879704
user2879704

Reputation:

Select next table row using next() gives undefined

I have two rows in my table as shown in this jsfiddle link, http://jsfiddle.net/o6ew3zom/.

I select all rows using table tr and access the next row as,

$(this).next()

It prints,

the current element id is  firstRow
the next element id is undefined
the current element id is  secondRow
the next element id is undefined

I expect,

    the current element id is  firstRow
    the next element id is secondRow
    the current element id is  secondRow
    the next element id is undefined

Upvotes: 0

Views: 478

Answers (2)

c-smile
c-smile

Reputation: 27470

JQuery.next() function returns next sibling element.

If you need to get all rows in table use this:

var allrows = $(this).closest("table").find("tr");

Upvotes: 1

guradio
guradio

Reputation: 15565

Because one is in the thead and the other is in the tbody. The one in the thead has no next element and the one in the tbody has no next element as well. But if you try to remove the thead and tbody it will then show you the id of the next element of the first one as shown in the fiddle.

demo

<table>
    <tr id="firstRow">
        <th>state</th>
        <th>compound</th>
    </tr>
    <tr id="secondRow">
        <td>solid</td>
        <td>iron</td>
    </tr>
</table>

Upvotes: 1

Related Questions