Search code examples
jquerytogglechaining

jQuery chaining and toggle not running


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();

Solution

  • 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