Ah I found it myself.. quite easy actually.

At the top change:
return this.each(function(i, elem){
  console.log(printSettings());
});

in something like this:

if(! this.selector ){ // accessed by $.plugin()
  // here you can init the plugin without selected element
}
else { // accessed by $('').plugin()
  return this.each(function(i, elem){
    // here you can return each
  });
}

This is handy, cause now you can also call your plugin's functionality
from javascript, instead of directly from an event.




On 30 jul, 11:23, publicJorn <publicj...@gmail.com> wrote:
> Hi all, I'm kinda new to these forums, but I was hoping someone could
> help me out here..
>
> I've been making plugins in a particular format that suits me really
> well. But now I want to be able to run it without using the jquery
> selector.
> Kind of like you can use: $('p').each(function(){}) ;
> I want to be able to do: $.each(array, function(){});
>
> Anyone know how to do this?
>
> Here's the plugin layout that I use. See the second last line for my
> attempt to create this functionality.. But this sometimes gives an
> error "too much recursion" or it displays all jquery functions
> (problably because it's iterating over the jquery object).
>
> /**
>  * Plugin testDirectAccessAccess
>  */
> (function($){
>   $.fn.testDirectAccess = function(options){
>     var settings = $.extend({}, $.fn.testDirectAccess.defaults,
> options);
>
>     return this.each(function(i, elem){
>       console.log(printSettings());
>     });
>
>     // function
>     function printSettings()
>     {
>       return settings;
>     }
>
>   } // end $.fn.testDirectAccess
>
>   /**
>    * Plugin defaults
>    */
>   $.fn.testDirectAccess.defaults = {
>     height: '100px',
>     width : '200px'
>   };
>
>   $.testDirectAccess = function(opts){ $.fn.testDirectAccess
> (opts); } // this thingy
>
> })(jQuery);

Reply via email to