2

ok, so what i'm trying to do is a plugin that returns a jquery array to be used in a callback function.

let's say that i have this code``

(function($){
$.fn.extend({
    //plugin name
    myPlugin : function(needed){

        var defaults = {
            path : 'action.php',
            source : '' 
        }
        var needed = $.extend(defaults,needed);

        //return
        return this.each(function(){
            //it loads some pictures
            $('#selector').load(needed.path,{'src':nedeed.source})

        });
    }
});

})(jQuery);

i want to return those pictures and have acces to them in a callback function. something like this

$('#another_selector').click(function(){
         $(this).myPlugin({'source':'path/...etc'},function(){
                 $('img').click(function(){
                       $(this).remove();
}); 
});
    });

thanks

2 Answers 2

1
(function($){
    $.fn.extend({
    //plugin name
    myPlugin : function(needed,callback){

            var defaults = {
                    path : 'action.php',
                    source : '' 
            }
            var needed = $.extend(defaults,needed);

            //return
            return this.each(function(){
                    //it loads some pictures
                    $('#selector').load(needed.path,{'src':nedeed.source},callback)

            });
    }
});

and wheni call the plugin i call it like this:

$('#another_selector').click(function(){
     $(this).myPlugin({'source':'path/...etc'},function(){
             $('img').click(function(){
                   $(this).remove();
 }); 
 });
  });

that function after the options represents the callback

Sign up to request clarification or add additional context in comments.

Comments

0

I see what you're trying to do. If this is all you're doing, you might consider just adding a live event listener to your plugin.

Try this:

(function($){
    $.fn.extend({
        //plugin name
        myPlugin : function(needed){
                // New
                $('img').live('click',function() {
                    $(this).remove();
                });
                // end new
                var defaults = {
                        path : 'action.php',
                        source : '' 
                }
                var needed = $.extend(defaults,needed);

                //return
                return this.each(function(){
                        //it loads some pictures
                        $('#selector').load(needed.path,{'src':nedeed.source})

                });
        }
    });
})(jQuery);

With that technique, all img's, no matter when they are added to the DOM will be hidden when clicked. That is... after the plugin is called, of course.

2 Comments

ok, so my plugin is more complicated than that but i just simplified it to be more comprehensive. i'll try what you said and i'll let you know if your suggestion works for me. thanks
on mouseOver for each image i want a hover action(appear/disappear an edit bar). in this case how should i do it? something like this? $('img').live('mouseenter',function(){ $(this).hover( function(){ pops the edit bar}, function(){ the edit bar is hidden } ); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.