Reputation: 1129
I am building a little plugin for jquery and on the default options I want to give if it should be used "bind" or "live" for the click event. So I have
var defaults = { method: 'bind' /* or live */ };
var options = $.extend(defaults, options);
and the plugin continues
$(selector).**method**('click', function(event) { /* code */});
Now, how can I make that the method is chosen by the default method value, without having to write the following.
if(options.method == 'live') { $(selector).live('click', function(event) { /* code */}); } else { $(selector).bind('click', function(event) { /* code */});}
Thanks.
Upvotes: 1
Views: 65
Reputation: 18344
You should do this:
$(selector)[options.method]('click', function(event) {
/* code */
});
Cheers
Upvotes: 2