I have constructed a JavaScript for loop with the run condition set to the length of an options Collection. The length of the options Collection is verified at 27 through console.log before the loop.
But when I run the loop it seems to run only 14 times - the loop removes an option each iteration, and when it's done there are 13 options left. Also I'm running console.log after the loop.
Here is a JSFiddle.
And here is my for loop:
(function () {
var industryOptions = document.getElementsByName("industry")[0].options;
console.log(industryOptions.length);
for (var k = 0; k < industryOptions.length; k++) {
industryOptions.remove(0);
}
console.log(industryOptions.length);
})();
NOTE: When I change the condition from industryOptions.length
to 27, it works as intended.
Why is my for loop stopping early after 14 loops instead of the entire industryOptions.length
? Thanks!