views:

180

answers:

3

If I run this code,

var waitRow = $(this).parent().parent().next().get(0);
$(waitRow).children('td:nth-child(2)').html('some text').toggle();

toggle is not called.

If I instead write the following code it works. Why?

var waitRow = $(this).parent().parent().next().get(0);
$(waitRow).children('td:nth-child(2)').html('some text');
$(waitRow).toggle();
A: 

Maybe the first one tries to toggle the wrong dom object?

jitter
+3  A: 

Because you are toggling the child element, not waitRow, I believe you could use .end() for this:

$(waitRow).children('td:nth-child(2)').html('some text').end().toggle();

To go back to the parent. Or use .parent() again.

Reference: http://docs.jquery.com/Traversing/end

meder
What exactly does .end() do?
Ghommey
It descends back to the prior jQuery object from the object it's currently at.
meder
Ofcourse...i must be blind sometimes. Thank you!
Erik Z
Sure. Don't forget to select an answer now :)
meder
A: 

The first one tries to toggle the first item in the children('td:nth-child(2)') wrapped set. The html() method returns the first matched item and not the whole collection.

The second one toggles the whole row.

kgiannakakis