I’m trying to develop one of my first jQuery objects:
jQuery(function ($) {
var WNM = function (element) {
var elem = $(element);
var obj = this;
var MultipleValues = function (element) {
var elem = $(element);
var obj = this;
var opt; // Options;
var sep; // Seperator string;
var vals; // Array wich holds the values;
//$(obj).find(".multipleValuesSett_MaxItems").val();
this.MultipleValues = function (options) {
opt = options;
sep = $(obj).find(".multipleValuesSett_Seperator").val();
loadValues();
outputValues();
};
var loadValues = function () {
var values = $(obj).find(".multipleValues_Values").val();
vals = values.split(sep);
};
var outputValues = function () {
var temp;
if ($.isFunction(opt.output)) {
temp = opt.output.apply(this, vals);
$(obj).find(".multipleValues_ValuesOutput")
.append(temp);
}
};
};
this.MultipleValues = function (options) {
if (elem.length) {
var mv = new MultipleValues(elem);
mv.MultipleValues(options);
return mv;
}
else
return null;
};
};
$.fn.MultipleValues = function () {
return wnm = new WNM(this);
};
});
Now whenever I try to run this:
$(document).ready(function () {
$(".multipleValues").WNMedia().MultipleValues({
output: function (data) {
var temp = "";
temp += "<ul>";
$.each(data, function (i) {
temp += "<li>" + data[i] + "</li>";
});
temp += "<ul>";
return temp;
}
});
});
I get the following error: (TypeError: $(".multipleValues").WNMedia() is not a function). Can someone help me out here please, I’m kind of stuck..
=========================================
Now I have another issue.. You can see that in de defined output function, I call the option output function. A param vals is passed, which is an array of strings. Whenever I loop through the data var, it contains only one item, while in fact there are three.. Any ideas?
Solved the issue, changed:
temp = opt.output.apply(null, vals);
into:
temp = opt.output.apply(null, [vals]);
you never defined WNMedia(), your sample works in following