Accessing a class variable with a string name?
2 answers
Try something like:
$.fn.dimeBar = function(custom) {
var options = {
var1: 'test1',
var2: 'test2'
};
if(sometest){
var foo = 'var1';
alert(options[foo]); // "test1"
}
};
Alternatively, this can be a little more restrictive with the jQuery plugin development pattern
(function($){
$.fn.dimeBar = function(options){
// defaults
options = $.extend({
var1: "default",
var2: "hello world"
}, options);
// debug options
$.each(options, function(key, value){
console.log(key+' is set to'+value);
});
};
})(jQuery);
$('#foo').dimeBar({var2: "hello kitty"});
// console output
// var1 is set to default
// var2 is set to hello kitty
+2
a source to share