jQuery Selector - Loop through set of elements without the .length method

jQuery Selector - Loop through set of elements without the .length method

by Nirvanachain

HTML

<p class="test">1</p>
<p class="test">2</p>
<p class="test">3</p>

JavaScript

(function () {
    'use strict';
    
    var list = $('.test'),
        text; //text is a variable declared outside the for-loop but used inside the for-loop

    for (var i = 0, item; item = list[i]; i++) {
    
        console.log('Looping: i = ' + i, ' item = ' + item);
     
        if (i === 1) {
            //Add new class attribute.
            item.className = item.className + ' hello';
        };
    
        //ie7 & ie8 do not support textContent.  Moder Browsers support .textContent
        text = item.innerText || item.textContent
        console.log('text = ' + text);
    
        item.innerHTML = text + ' ' + item.className;
    };
    
}());