Many JavaScript books recommend using a for
loop when dealing with an Array
of objects to perform a certain action.
function()
{
var links = document.getElementsByTagName("a");
for (var i = 0, ii = links.length; i < ii; i++)
{
links[i].addEventListener("event", eventListener, false);
}
}
But why do we use a for
loop? Why not simply manipulate the Array
directly like in the code below?
function()
{
var links = document.getElementsByTagName("a");
links.addEventListener("event", eventListener, false);
}
I apologize for any language mistakes.