javascript / jquery scope differences between jQuery.each and normal for loop?
<p>Interesting issue here, optimising some code but am having trouble with scope issues using a javascript loop as opposed to a jQuery loop.</p>

<p>If for example we have 3 "decade" iterations, with values "1950", "1960", and "1970", using the javascript loop, we would get "1970" (the last iteration in the loop) all the time on the "alert" of decade.name, but using jQuery.each we get the expected value for each created element. </p>

<p>Questions:
- why does this work with jQuery.each and not regular javascript loop?
- how can i make this work with a normal javascript for loop?</p>

<pre><code>$.ajax({
    async: true,
    type: 'post',
    url: 'ajax/products.php',
    data: {
        'action': 'get_filter_decades',
        'data': {
            "search_term": ple.find('input[name="search_term"]').val(),
            "first_letter": $.bbq.getState('first_letter'),
            "category_id": categoryId.val(),
            "original_release_year_range": ple.find('input[name="original_release_year_range"]').val()
        }
    },
    success: function (jsonResponse) {
        if (jsonObj = handleJsonResponse(jsonResponse)) {
            var filterDecadesResultList = filterDecades.find('ul.decade_result_list');
            filterDecadesResultList.hide();
            filterDecadesResultList.empty();

            var originalReleaseYearRange =     $(ple.find('input[name="original_release_year_range"]'));

            // jquery loop
            //$( jsonObj.data.decades ).each( function( i, decade ) {

            // javascript loop (faster!)
            var decadesLength = jsonObj.data.decades.length;
            var listElement;
            var linkElement;
            var decade;

            for (var i = 0; i &lt; decadesLength; i++) {
                decade = jsonObj.data.decades[i];
                listElement = $('&lt;li /&gt;');
                linkElement = $('&lt;a /&gt;');
                linkElement.text(decade.name);

                if (decade.product_count &gt; 0) {
                    linkElement.append(' (' + decade.product_count + ')');
                    if (decade.selected) {
                        linkElement.addClass('selected');
                    }
                    linkElement.on("click", function () {
                        alert(decade.name);

                        if (!linkElement.hasClass('selected')) {
                            originalReleaseYearRange.val(decade.name);
                            $.bbq.pushState({
                                'original_release_year_range': decade.name
                            });
                            productListPaginate(ple);
                        } else {
                            originalReleaseYearRange.val('');
                            $.bbq.removeState('original_release_year_range');
                            productListPaginate(ple);
                        }
                    });
                }

                listElement.append(linkElement);
                filterDecades.find('ul.decade_result_list').append(listElement);
                // jquery loop
                //});

                // javascript loop
            }

            filterDecadesResultList.show();
        }
    }
});
</code></pre>
