I would like use .appendTo() on different elements determined by a function. For example, I want to be able to do:
$myElement.appendTo(function () {
if(Math.random() < 0.5) return $el1; else return $el2;
});
However, the jQuery documentation doesn’t cover the case of a function given as an argument to .appendTo().
Is there a specific reason why the jQuery team would not allow for such a feature, and is there a smart way around it (maybe by redefining the .appendTo() function)? Many other jQuery functions allow functions as argument…
There are many ways to make your code work :
appendTocallbut if you truly want to make the jquery
appendTomethod to take a function as a parameter, you should extend theappendTomethod yourself :then you can pass a function as parameter to
appendTo:Here’s a working example :
http://jsfiddle.net/gion_13/ZEnMQ/