# Help with this.each() inside jQuery plugin

**URL:** <https://forum.kirupa.com/t/help-with-this-each-inside-jquery-plugin/274275>\
**Category:** web dev\
**Created:** [October 27, 2008, 10:15pm UTC](https://forum.kirupa.com/t/help-with-this-each-inside-jquery-plugin/274275 "2008-10-27T22:15:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SpawnOfJon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/spawnofjon/32/3308_2.png) [@SpawnOfJon](https://forum.kirupa.com/u/SpawnOfJon)\
**Post date:** [October 27, 2008, 10:15pm UTC](https://forum.kirupa.com/t/help-with-this-each-inside-jquery-plugin/274275/1 "2008-10-27T22:15:28Z")

</div>

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?

```auto
$.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!
