Bug Tracker

Modify

Ticket #6856 (closed enhancement: duplicate)

Opened 3 years ago

Last modified 3 years ago

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:
Blocking: Blocked by:

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, 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:

$("<hr>").appendTo(".foo", context).click(...)

Change History

comment:1 Changed 3 years ago by dmethvin

  • Component changed from unfiled to manipulation

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

comment:2 Changed 3 years ago by dmethvin

  • Status changed from new to closed
  • Resolution set to duplicate

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

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.