How can I copy the stack of a jQuery object to another jQuery object, so I can use end in my plugins even when returning totally unrelated objects? Example:
$(myselector)
    .next()             // Destructive operation
        .doSomething()
    .end()              // Goes back to "myselector"
    .doSomethingElse(); // Works fine!
$.fn.myPlugin = function() {
    return $(unrelated); // No stack, can't call "end" on it
};
$(myselector)
    .myPlugin()         // Destructive operation
        .doSomething()
    .end()              // Goes back to nowhere, since the stack is empty
    .doSomethingElse(); // Doesn't work
I'd like to modify the $(unrelated) object to include this's stack, so the second example would work. Here's a complete example in jsFiddle.
$.fn.myPlugin = function(related) {
    if ( related == "getRelated" )
        return this.pushStack($(this.data("related")).get()); // Want to preserve stack
    return this.data("related",related);
};
You need to push the element to the stack so that end() gets back to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With