This is a bit odd. I was testing an idea when I ran into a strange "undefined" error.
The following javascript outputs info exactly as it should at the end of each loop, but I also get an "allinputs is undefined" error on the first "if" inside the "for" loop.
function Init() {
var allinputs = document.body.getElementsByTagName('input');
var textbox = 0;
var passbox = 0;
var radiobox = 0;
var checkbox = 0;
document.getElementById('testing').innerHTML = allinputs.length + '<br>';
for (var i = 0; i <= allinputs.length; i++) {
if (allinputs[i].getAttribute('type') == 'text') {
textbox++;
}
if (allinputs[i].getAttribute('type') == 'checkbox') {
checkbox++;
}
if (allinputs[i].getAttribute('type') == 'radio') {
radiobox++;
}
if (allinputs[i].getAttribute('type') == 'password') {
passbox++;
}
document.getElementById('testing').innerHTML += i + ' ' + textbox + ' ' + checkbox + ' ' + radiobox + ' ' + passbox + '<br>';
}
}
I have tried removing the first "if" as well as testing for undefined. Actually when I wrapped all the if's in an "if" to checked if "i" was undefined and if true it set "i" to equal 1 it caused my browser to crash. Its almost like "i" is both defined and undefined.
Now the above code is actually a mistake as I intended the "document.getElementById('testing').innerHTML" at the end of the for loop to be after the for loop. And if I do that I get no output and I still have that error.
What is causing the "allinputs is undefined" error?