De-Anonymizing an Anonymous Function in jQuery
<p>This is probably quite a simple problem, but it's causing me to scratch my head, so I'm posting it here.</p>

<p>I have some jQuery in the following form:</p>

<pre><code>if (jQuery('.SearchRegions:checked').length == 0) { 

jQuery('.SearchRegions').each(function(){

        //code

    });

} else {

jQuery('.SearchRegions:checked').each(function(){

        //the same code     

    });

}
</code></pre>

<p>Obviously it seems ridiculous to repeat a big block of code inside each of these functions.  But when I tried to name and move the function, it all seemed to break down - perhaps because of issues with scope and/or jQuery(this) inside the function no longer referring to the same object?</p>

<p>Can anyone help me by posting a general idea of what my code should look like? (Or any other optimisings or recastings to make it work would be much appreciated!)</p>
