I’ve tried to make my first jquery plugin (a lightbox). It works on a single element just the way I want but if I use multiple elements it overwrites my options.
here is my current code. it doesn’t seem to work on jsFiddle because of the layout.
you can download the code with a working (failing) sample here:
http://frumbert.org/files/frumbox.zip
I can see that I’m somehow overwrite the options in the default object when I bind to a second object but don’t know what i’ve done wrong. I’ve tried moving where click is bound but can’t make it work.
$this, declared in the outermost closure and used throughout, represents the last element on which FrumBox is instantiated.
Personally, I wouldn’t try to fix the code as it stands. Instead adopt jQuery’s “Best Practices” pattern. Here’s my template based on the example given:
Notes:
return this.each(function(index){...})pattern in every method, except any that return a specific value.thisrefers to in every context. Keep your wits about you.Function.call()andFunction.applymethods. Make sure you understand them.modified. It is a cool chunk of code.
$(selector).pluginNmae()or$(selector).pluginNmae(options), whereoptionsis an object literal “map”.$(selector).pluginNmae('methodName', ...)