[jQuery] Re: Returning a new collection from a method
Simply return your new collection. How you get your new collection from the current object may well involve map(), but however you do it, all you need to do is return whatever collection you want to continue the chain with. It is probably advisable to use pushStack(), so that your original collection can be retrieved further along the 'chain' if desired (by using end()). eg. $.fn.myMethod = function() { return this.pushStack( this.map(function(i,elem) { // // return newElement // }) ); }; or $.fn.myMethod = function() { var returnObject = []; this.each(function() { // var newElement = something based on 'this' // returnObject.push(newElement); }); return this.pushStack(returnObject); }; or variations thereof. On Dec 12, 3:01 am, McLars <[EMAIL PROTECTED]> wrote: > I have a plugin that needs to return a different jQuery collection > than the one it was passed. In other words, each of the elements > passed in the original collection will create a new element, and I > want to return the new elements instead of the originals. > > So, instead of something like this: > > $.fn.myMethod = function() { > return this.each(function() { > // > // Create new element here > // > > }); > > which returns the original element collection (preserves chaining), > I'm looking for an elegant way to return the new elements in a jQuery > collection. > > I think maybe the $.map method might be part of the solution, but I'm > afraid I just can't understand how. Any help would be appreciated. > > Thanks, > > Larry
[jQuery] Re: Returning a new collection from a method
I could not get the first example to work. Again, I just can't seem to grasp what map does or how to make it work. The second example is very close to working, except that it returns an array, not a jQuery collection. So, let me try rephasing my question. How do I create an empty jQuery collection? I have actually acomplished this, but it's very ugly. Instead of the following: var returnObject = []; //creates an empty array I used the following: var returnObject = $(this).not(this); That seems pretty stupid to me, but it's the only way I could figure out to create an empty jQuery collection, to which I can add individual jQuery objects from inside my loop. Surely, there is a more elegant way to create an empty jQuery collection. The pushStack suggestion is great, btw. Thanks again, Larry On Dec 12, 4:42 am, Wizzud <[EMAIL PROTECTED]> wrote: > Simply return your new collection. How you get your new collection > from the current object may well involve map(), but however you do it, > all you need to do is return whatever collection you want to continue > the chain with. It is probably advisable to use pushStack(), so that > your original collection can be retrieved further along the 'chain' if > desired (by using end()). > > eg. > > $.fn.myMethod = function() { > return this.pushStack( this.map(function(i,elem) { > // > // return newElement > // > }) ); > > }; > > or > > $.fn.myMethod = function() { > var returnObject = []; > this.each(function() { > // var newElement = something based on 'this' > // returnObject.push(newElement); > }); > return this.pushStack(returnObject); > > }; > > or variations thereof. > > On Dec 12, 3:01 am, McLars <[EMAIL PROTECTED]> wrote: > > > > > I have a plugin that needs to return a different jQuery collection > > than the one it was passed. In other words, each of the elements > > passed in the original collection will create a new element, and I > > want to return the new elements instead of the originals. > > > So, instead of something like this: > > > $.fn.myMethod = function() { > > return this.each(function() { > > // > > // Create new element here > > // > > > }); > > > which returns the original element collection (preserves chaining), > > I'm looking for an elegant way to return the new elements in a jQuery > > collection. > > > I think maybe the $.map method might be part of the solution, but I'm > > afraid I just can't understand how. Any help would be appreciated. > > > Thanks, > > > Larry- Hide quoted text - > > - Show quoted text -
[jQuery] Re: Returning a new collection from a method
pushStack() should return its argument as a jQuery object. So if you give it an array of elements it should come back as a jQuery 'collection' of those elements. $([]) gives an empty jQuery object. On Dec 12, 7:49 pm, McLars <[EMAIL PROTECTED]> wrote: > I could not get the first example to work. Again, I just can't seem to > grasp what map does or how to make it work. > > The second example is very close to working, except that it returns an > array, not a jQuery collection. So, let me try rephasing my question. > How do I create an empty jQuery collection? > > I have actually acomplished this, but it's very ugly. Instead of the > following: > > var returnObject = []; //creates an empty array > > I used the following: > > var returnObject = $(this).not(this); > > That seems pretty stupid to me, but it's the only way I could figure > out to create an empty jQuery collection, to which I can add > individual jQuery objects from inside my loop. Surely, there is a more > elegant way to create an empty jQuery collection. > > The pushStack suggestion is great, btw. > > Thanks again, > > Larry > > On Dec 12, 4:42 am, Wizzud <[EMAIL PROTECTED]> wrote: > > > Simply return your new collection. How you get your new collection > > from the current object may well involve map(), but however you do it, > > all you need to do is return whatever collection you want to continue > > the chain with. It is probably advisable to use pushStack(), so that > > your original collection can be retrieved further along the 'chain' if > > desired (by using end()). > > > eg. > > > $.fn.myMethod = function() { > > return this.pushStack( this.map(function(i,elem) { > > // > > // return newElement > > // > > }) ); > > > }; > > > or > > > $.fn.myMethod = function() { > > var returnObject = []; > > this.each(function() { > > // var newElement = something based on 'this' > > // returnObject.push(newElement); > > }); > > return this.pushStack(returnObject); > > > }; > > > or variations thereof. > > > On Dec 12, 3:01 am, McLars <[EMAIL PROTECTED]> wrote: > > > > I have a plugin that needs to return a different jQuery collection > > > than the one it was passed. In other words, each of the elements > > > passed in the original collection will create a new element, and I > > > want to return the new elements instead of the originals. > > > > So, instead of something like this: > > > > $.fn.myMethod = function() { > > > return this.each(function() { > > > // > > > // Create new element here > > > // > > > > }); > > > > which returns the original element collection (preserves chaining), > > > I'm looking for an elegant way to return the new elements in a jQuery > > > collection. > > > > I think maybe the $.map method might be part of the solution, but I'm > > > afraid I just can't understand how. Any help would be appreciated. > > > > Thanks, > > > > Larry- Hide quoted text - > > > - Show quoted text -