Javascript function "return this" not working
<p>I have the following function, it just runs through a list of objects and return the correct one:</p>

<pre><code>function findLine(textElement,caretIndex){
    jQuery.each(textElement.lines(), function() {
        if(this.startIndex &lt;= caretIndex &amp;&amp; this.endIndex &gt;= caretIndex) {
            alert(this);
            return this;
        }
   });
}
</code></pre>

<p>When I call it with this I get <code>undefined</code> in return.</p>

<pre><code>line = findLine(textElement,caretIndex);
alert(line);
</code></pre>

<p>Oddly enough when I run <code>line = findLine(textElement,caretIndex);</code> the alert within the function is fired and return the correct results. So <code>this</code> is the correct value, however when the second alert outside the function is fired I get <code>undefined</code>.</p>

<p>Either there's something wrong happening when I'm returning the value from the function or it's something to do with assigning that value to a variable. What am I doing wrong here?</p>
