Bug Tracker

Ticket #8176 (closed enhancement: wontfix)

Opened 4 years ago

Last modified 4 years ago

appendTo should accept 'context' parameter

Reported by: niloy_mondal@… Owned by: niloy_mondal@…
Priority: low Milestone: 1.next
Component: manipulation Version: 1.5
Keywords: Cc:
Blocking: Blocked by:

Description

appendTo() should accept 'context' parameter so that it is possible to do something like this:

select = $("<select>").addClass("choices"); $("<option>").text("Hello").appendTo(".choices", select);

Change History

comment:1 Changed 4 years ago by jitter

  • Owner set to niloy_mondal@…
  • Priority changed from undecided to low
  • Status changed from new to pending
  • Component changed from unfiled to manipulation
  • Type changed from feature to enhancement

Thanks for taking the time to contribute to the jQuery project by writing an enhancement request.

Unfortunately your request is rather scarce, you don't go into detail how this modification should work is supposed to behave. You also don't provide any use cases (2-3 at least would be good) where this would prove to be useful to the vast majority of the jQuery user base.

The missing explanation on why this is needed and is something that can't be done (or only with difficulty) with the current available method signatures in jQuery makes it difficult to judge the validity of this request.

Also I don't really understand the intention of the code snippet you added to your description. Isn't the following snippet already achieving what you are aiming for by just using the currently available method signature?

var select = $("<select>").addClass("choices");
$("<option>").text("Hello").appendTo(select);
Version 0, edited 4 years ago by jitter (next)

comment:2 Changed 4 years ago by niloy_mondal@…

  • Status changed from pending to new

Thank you for the review.

appendTo currently assumes the window DOM as the 'context' and provides no way to specify otherwise. $() accepts the second parameter as the context.

The most common use case would be when using $.tmpl().

var page = $("#template").tmpl(object);

//Say the template contains a select box with id="country", but its options 
//are going be populated using ajax

$.getJSON('get-countries.php', function(countries){
    for (var i = 0; i < countries.length; i++){
        $("<option>").text(countries[i]).appendTo("#country", page);
        
        //Right now I have to do it like this...
        //var opt = $("<option>").text(countries[i]);
        //$("#country", page).append(opt);
    }
});

comment:3 Changed 4 years ago by rwaldron

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

Take a look at the docs:  http://api.jquery.com/appendTo/

Scroll to this: $('h2').appendTo($('.container'));

And then, take a look at this:  http://jsfiddle.net/rwaldron/GxBAt/2/

I think you'll find this meets your needs! Thanks again!

Last edited 4 years ago by rwaldron (previous) (diff)

comment:4 Changed 4 years ago by jitter

Replying to niloy_mondal@…:

Thank you for the review.

appendTo currently assumes the window DOM as the 'context' and provides no way to specify otherwise. $() accepts the second parameter as the context.

Did you even try the code snippet I provided above? It basically does the same as the code provided rwaldron

Another  test case variation.

Last edited 4 years ago by jitter (previous) (diff)
Note: See TracTickets for help on using tickets.