tags:

views:

46

answers:

1

When I say, msg.appendTo(ele.parent().next()), the msg successfully gets appended to a <p> with class=foo

How can I specify it explicitly in the statement?

I tried msg.appendTo(ele.parent().next().find('.foo'));

but it doesn't work

+3  A: 

Your question is not totally clear, but maybe you are looking for .siblings():

msg.appendTo(ele.parent().siblings('.foo'));

Explanation:

Your code msg.appendTo(ele.parent().next()) will append msg to the next sibling of the parent element of ele.

Whereas msg.appendTo(ele.parent().next().find('.foo')) will append msg to all elements with class foo inside the next sibling of the parent element of ele.


Whenever you are uncertain about how a method works, read the documentation first. Most questions will be covered by it.

Documentation of .appendTo().

Also you should probably read about how .next(), .find(), etc. work.

Felix Kling
and can I use appendTo specifying the id of the element directly, without navigating?
Gokul
@Gokul: Of course, you can do `msg.appendTo('#id');` Just read the [documentation](http://api.jquery.com/appendTo/) and have a look at the examples. It is all very well explained.
Felix Kling