jquery: Writing a method
<p>This is the same question as this:
<a href="http://stackoverflow.com/questions/2890620/jquery-running-a-function-in-a-context-and-adding-to-a-variable">http://stackoverflow.com/questions/2890620/jquery-running-a-function-in-a-context-and-adding-to-a-variable</a></p>

<p>But that question was poorly posed.</p>

<p>Trying again, I'm trying to do something like this:</p>

<p>I have a bunch of elements I want to grab data from, the data is in the form of classes of the element children.</p>

<pre><code>&lt;div id='container'&gt;
    &lt;span class='a'&gt;&lt;/span&gt;
    &lt;span class='b'&gt;&lt;/span&gt;
    &lt;span class='c'&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div id='container2'&gt;
    &lt;span class='1'&gt;&lt;/span&gt;
    &lt;span class='2'&gt;&lt;/span&gt;
    &lt;span class='3'&gt;&lt;/span&gt;
&lt;/div&gt;
</code></pre>

<p>I have a method like this:</p>

<pre><code>jQuery.fn.grabData = function(expr) {
    return this.each(function() {
        var self = $(this);             
        self.find("span").each(function (){
            var info = $(this).attr('class');
            collection += info;
        });
    });
};
</code></pre>

<p>I to run the method like this:</p>

<pre><code>var collection = '';
$('#container').grabData();
$('#container2').grabData();
</code></pre>

<p>The collection should be adding to each other so that in the end I get this</p>

<pre><code>console.log(collection);
</code></pre>

<p>:</p>

<pre><code>abc123
</code></pre>

<p>But collection is undefined in the method. How can I let the method know which collection it should be adding to?</p>

<p>Thanks.</p>
