Ticket #6856 (closed enhancement: duplicate)
Should .appendTo accept a context for the selector?
|Reported by:||phyzome||Owned by:|
It is very common to create a DOM fragment, attach it to the DOM, and further manipulate it. Such manipulation often takes this form:
(Please note the extra work done in the first example.)
Sometimes, though, the selector for the bit of the DOM to attach to is more complicating, perhaps requiring .find-like behavior. The first example is easily modified. Here, context is some DOM element from the environment:
$(".foo", context).append("<hr>").find("> hr:last-child").click(...)
The second, example, is not as easy. .appendTo does not take a second argument, leading to more verbose code:
var deeper = $(".foo", context); $("<hr>").appendTo(deeper).click(...)
Observe that there is no clean way to create and append a fragment and keep working with it when the DOM attachment point requires context.
I propose that appendTo be given the ability to take a context element with the filter, leading to beautiful code like this: