Why can't I use the return from an insertAfter as a regular jQuery object?

Posted by joachim on Stack Overflow See other posts from Stack Overflow or by joachim
Published on 2010-04-04T10:12:30Z Indexed on 2010/04/04 10:23 UTC
Read the original article Hit count: 381

Filed under:

I'm trying to insert a link after form elements to clear them.

This demo code with headings doesn't work:

h2 = $('h2'); clickytest = $('click me').insertAfter(h2).click(function() { $(this).append('foo'); });

But this does:

clickytest = $('click me').insertAfter(h2); $('a.clicky').click(function() { $(this).append('foo'); });

The only difference is I've gone back and re-selected the new elements, rather than use what insertAfter returns.

If on the other hand there is only one H2 in the whole document, then the first version works. What's going on? I've tried playing with each() but I'm not sure exactly what jQuery is doing here.

© Stack Overflow or respective owner

Related posts about jquery-ui