Looks like you solved it yourself; your second version is much closer to the
mark.

Do you actually write your code unindented like that, though, or did the
indentation just lost in the process of posting it? I would format your code
more like this:

    $.fn.toggleAnim1 = function(fn){
        var $obj = this;
        $obj.each(function(){
            $obj.animate({'opacity': 'toggle', 'height': 'toggle'}, 'slow',
function() {
                $.isFunction(fn) && fn();
            });
        });
    };

Now that I can read your code, I see a problem. You are calling $obj.each to
iterate through every element in the $obj collection. And then inside that
loop, you are calling $obj.animate(), which will animate *every* element in
the collection.

If there is only a single element, e.g. $('#foo'), that won't matter. If
there are multiple elements, then each one will be animated multiple times,
probably not what you want!

I can't really suggest a solution, though, because I don't know what the
code is supposed to do. Can you describe exactly what you want to have
happen when there are multiple elements in the jQuery collection?

-Mike

> From: Micky Hulse
> 
> Ah, this is better:
> 
> $.fn.toggleAnim1 = function(fn){
> var $obj = this;
> $obj.each(function(){
> $obj.animate({'opacity': 'toggle', 'height': 'toggle'}, 
> 'slow', function
> () {
> $.isFunction(fn) && fn();
> });
> });
> };
> 
> Obviously, I am kinda a plugin noob... Feel free to give tips! :)
> 
> Just ordered jQuery Reference Guide, so hopefully I can stop 
> bugging all ya'll. :D
> 
> Cheers,
> Micky
> 
> Micky Hulse wrote:
> > Hey there!
> > 
> > Consider this code:
> > 
> > $.fn.toggleAnim1 = function(f){
> > this.animate({'opacity': 'toggle', 'height': 'toggle'}, 'slow',
> > function() { return f; });
> > }
> > 
> > $('#success').toggleAnim1(function() { 
> alert('hizzzzzzzzzzzzzz'); });
> > 
> > Is that one way to provide callback functionality?
> > 
> > I would love some pro advice. :)
> > 
> > Thanks in advance!
> > Cheers,
> > Micky
> 

Reply via email to