Can you please look at this:
(function ($) {
$.fn.extend({
grid: function (settings) {
var defaults = {
data: [
['Code','Name','Email','Other'],
[1,'John','john@domain.com','Johny'],
[2,'Bob','bob@domain.com','Bobby'],
[3,'Jenny','jenny@domain.com','Jen'],
[4,'Mary','mary@domain.com','Maryann']
],
test: this.data.length
};
var config = $.extend(defaults, settings);
return this.each(function(){
this.innerHTML = config.data.length;
this.innerHTML += "<br />" + config.test;
});
}
});
}(jQuery));
$('.content').grid();
And explain why the two results are different?
Also how do I achieve the desired result?
The problem is that when you call
this.data.length, it’s actually counting the length of the jQuerydatafunction and not the length ofdefaults. Additionally, you can’t count the length ofdefaults.datauntil after it’s been assigned.Have a look at this working example: http://jsfiddle.net/w8PG6/1/