On Dec 21, 4:06 pm, Micky Hulse <rgmi...@gmail.com> wrote: > Ok, so how does this look: > > (function($) { > $.fn.myFunction = function(id) { > var $target = $('#' + id); > if($target.length > 0) { > return this.each(function() { > // Do stuff here. > }); > } // $target > }; > })(jQuery); > > $('#div').myFunction('targ');
Looks right to me. One alternative, which wouldn't break the chain if target is not supplied properly, would be this: (function($) { $.fn.myFunction = function(id) { var $target = $('#' + id); if ($target.length != 1) return this; return this.each(function() { // Do stuff here. }); }; })(jQuery); > My plugin will only be used on an #id, and the output will only > manipulate a unique #id... In other words, there will only be one > unique id in both cases. With that said, is it overkill to use > "this.each()"? If so, what is the alternative syntax? I don't think it's overkill for your initial jQuery object. I would suggest that you don't do anything with .each for the target; I can't see any reason for that. jQuery.each() doesn't add much overhead. If you really wanted an alternative syntax, you might simply work with this[0], assuming it exists. In jQuery, that will be the first matched element. But this leads to a more fundamental question, of why would this be a plug-in if it only works on a single DOM element, presumably with an id selector? Why not a plain JS function of the following form?: function myFunction(source, target) You could still use jQuery inside the function. Perhaps it's just to learn jQuery plug-ins, and there's nothing wrong with that. But if it doesn't work on multiple elements, should it really be a plug-in? Food for thought, anyway. Good luck, -- Scott