Bug Tracker

Opened 13 years ago

Closed 13 years ago

#6856 closed enhancement (duplicate)

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:


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



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

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

Change History (2)

comment:1 Changed 13 years ago by dmethvin

Component: unfiledmanipulation

This looks like a trivial-to-do enhancement for all the other-way-round appenders (appendTo, prependTo,insertBefore, insertAfter, replaceAll).

comment:2 Changed 13 years ago by dmethvin

Resolution: duplicate
Status: newclosed

Duplicate of #5065, which has a patch. I'll try to shepherd this into the next release.

Note: See TracTickets for help on using tickets.