jQuery plugin options: required, optional, inaccessible

Posted by Trevor Hartman on Stack Overflow See other posts from Stack Overflow or by Trevor Hartman
Published on 2010-04-27T02:52:35Z Indexed on 2010/04/27 2:53 UTC
Read the original article Hit count: 354

I'm curious how to specify options to a jQuery plugin in a way that some are required, some are optionally overridden, and some can't be touched.

I started off with the usual:

jQuery.fn.plugin = function (options){
  var defaults = { username: "", posts:10, api: "http://myapi.com" }
  var settings = jQuery.extend({}, defaults, options);
}

Let's say I want username to be required, posts is optional (defaults to 10) and you (you being the user of the plugin) can't change api, even if they try. Ideally, they'd all still be in the same data structure instead of being split into separate objects. Ideas?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-plugins