I’m building a plugin and am trying to get the standard ‘return this.each’ function to work, but to no avail. I know I must be missing something stupid. Can anyone see what I’m doing wrong?
$.fn.randomContent = function(options){
var defaults = {
// Set the path to your XML file
xmlPath: "chuck.xml",
// Define the class name that will recieve the random content
className: "chuck",
// Specify the node name in the XML file that contains the content
nodeName: "fact"
};
var options = $.extend(defaults, options);
return this.each(function() {
alert(this);
});
$.get(defaults.xmlPath, {}, function(xml){
$("."+defaults.className).each(function() {
var num = $(defaults.nodeName,xml).length
var randNum = Math.floor(Math.random()*num);
var txt;
$(defaults.nodeName,xml).each(function(i) {
if(i == randNum) {
txt = ($(this).text());
}
});
$(this).text(txt);
});
});
};
What I have works perfectly fine. But if I add those three lines, it breaks everything.
Thanks!