On Dec 21, 3:08 pm, Micky Hulse <rgmi...@gmail.com> wrote: > But, I guess I am wondering what the best way to handle javascript > error checking for required options?
There are a few options. I often add an optional errorHandler function; my defaults would include: errorHandler: function(status, message) {} and then in development, I can pass in $(selector).plugin({errorHandler: function(status, msg) {alert(msg + ": " + status);}); then pull it out for most production code. Then your code can check that the required parameter is there. if (!target) { opts.errorHandler(1234, "You forgot to pass a target"); return; } Alternatively, you can simply fail silently, but merely document that the parameter is required. > $('#div').myFunction({ target: '#targ' }); > If the only option is "target", can I do something like this instead: > $('#div').myFunction('#targ'); Absolutely, and I would recommend it. I'd also lose the "#" and just add it in in the code: $("#" + target).doSomething(); > Because "#targ" is a required argument, should I handle it differently > than your normal "options" object? Because it's the only required parameter, and in fact the only parameter, I would definitely treat it differently. If you had five required parameter and nine optional ones, they should probably all go into an options hash, and you would have to deal with the fact that some are required mainly in the documentation. I'm not sure where the dividing line would be, but a single mandatory parameter definitely falls in the simpler-is-better category. Cheers, -- Scott