JSLint throws the following error that I am not sure how to resolve:
Error: Don't declare variables in a loop.
Line: var text = item.innerText || item.textContent
My question is how do I get for support of innerText or textContent and use the correct method while looping through elements?
Here's my example. I am writing the class of the elements next to the text. I'd like to have the same results, but test for innerText or textContent outside of the for-loop.
Fiddle: http://jsfiddle.net/PwwjK/
HTML:
<p class="test">1</p>
<p class="test">2</p>
<p class="test">3</p>
JavaScript:
(function () {
'use strict';
var list = $('.test');
for (var i = 0, item; item = list[i]; i++) {
if (i === 1) {
//Add new class attribute.
item.className = item.className + ' hello';
};
var text = item.innerText || item.textContent
item.innerHTML = text + ' ' + item.className;
};
}());