Side navigation
#6856 closed enhancement (duplicate)
Opened July 31, 2010 10:10PM UTC
Closed August 11, 2010 01:26AM UTC
Should .appendTo accept a context for the selector?
Reported by: | phyzome | Owned by: | |
---|---|---|---|
Priority: | Milestone: | 1.4.3 | |
Component: | manipulation | Version: | 1.4.2 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
It is very common to create a DOM fragment, attach it to the DOM, and further manipulate it. Such manipulation often takes this form:
$("#foo").append("<hr>").find("> hr:last-child").click(...)
or
$("<hr>").appendTo("#foo").click(...)
(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,
contextis 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:
$("<hr>").appendTo(".foo", context).click(...)
This looks like a trivial-to-do enhancement for all the other-way-round appenders (appendTo, prependTo,insertBefore, insertAfter, replaceAll).