Syntax to combine parent() and next() to traverse table rows

Sailing_12
Sailing_12 used Ask the Experts™
on
What is the syntax to use jQuery parent() and next() functions together to traverse table rows.

I have a table with an image in the first celll of all of the odd rows which I want to use as a trigger to expand the corresponding next row. So from the image element, I guess I want to traverse up two parents (td and tr) and then get the next tr in the table.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Depending on your structure - your <img>'s parent is probably it's <td>, therefore your need to the next sibling of the parent of your <img>'s parent or:

$(this).parent().parent().next()
Sailing_12Pirate

Author

Commented:
Thanks. I just figured that out here but was wondering if there was a better way to code it.
The only other suggestion I can think of would be:

$(this).parents("tr").next()

(note the s on parents)
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Hello Sailing_12,

To be more explicit you can use the parents method to refer to all ancestor of the IMG and then filter them by the first TR.

You can do so as shown in the code snippet.

Hope it helps.

// Supposing 'this' is your image, then you will go
// to the first TR ancestor and then to the next TR.
$(this).parents('tr:first').next('tr');
 
// This will also work if there is only one TR 
// ancestor. But I recommend the previous.
$(this).parents('tr').next('tr');

Open in new window

Sailing_12,

Note, that the method parent is different than parents (with s). parent will only return the first ancestor of an element, but parents will return all ancestor without the root element.
Sailing_12Pirate

Author

Commented:
Great thanks.
Sailing_12Pirate

Author

Commented:
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial