views:

629

answers:

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

A: 

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.

KyleFarris
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
kmunky
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 } );});
kmunky
+1  A: 
(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

kmunky