13

Hey I'm trying to get list of all input fields in a HTML form, but I get following error(in Firebug):

ReferenceError: assignment to undeclared variable i
for (i=0 ; i<inputs.length; i++)

I don't understant how is "i" undeclared because that is that first part of "for" does. This is my formula

 function listinputs() 
    {
       var form = document.getElementById("wholeform");
       var inputs = form.childNodes;
       for (i=0 ; i<inputs.length; i++)
       {
          var string=string + inputs[i].nodeName + "<br>";
          var here = document.getElementsByTagName("p");
          here.innerHTML(string);
       }
}
4
  • 2
    for(var i = 0; ... you need to declare it with var
    – Kai Qing
    Apr 3, 2014 at 22:47
  • 2
    var here = document.getElementsByTagName("p"); here.innerHTML(string); should be outside your loop, by the way.
    – StackSlave
    Apr 3, 2014 at 23:09
  • 2
    @PHPglue: Yeah, outside the loop and the list returned by getElementsByTagName doesn't have an innerHTML property... Apr 3, 2014 at 23:10
  • I see that now. Yes that would be an Array, that you would have to run another loop on.
    – StackSlave
    Apr 3, 2014 at 23:12

3 Answers 3

29
for (var i=0; i<inputs.length; i++)

You need to declare it with var

As T.J said in his answer, since you're using strict mode, an implicit global is not made. That's why an error is thrown.

8
  • ow yeah I'm not sure why did I miss that. Probabliy due to 3w school example " for (i=0; i<c.length; i++) "
    – sgp667
    Apr 3, 2014 at 22:49
  • 3
    Yet another reason why w3schools should not be used. Apr 3, 2014 at 22:50
  • If it "needs" var, then why does for (q=0;q<10;q++) {q} Work Here? Apr 3, 2014 at 22:54
  • @RUJordan And that is the secret! See T.J.'s answer. Apr 3, 2014 at 22:55
  • Ah, that makes sense. strict mode == no globals == error. Apr 3, 2014 at 22:57
16

It's not saying i is unassigned, it's saying it's undeclared. The code never declares an i variable, but then tries to assign a value to it (in the initialization part of the for loop). Apparently you're using strict mode (good!), and so the engine is giving you an error rather than creating an implicit global.

Declare i using var in the function, e.g.:

function listinputs() 
{
    var form = document.getElementById("wholeform");
    var inputs = form.childNodes;
    var i; // <=================================== Here
    for (i=0 ; i<inputs.length; i++)
    {
        string=string + inputs[i].nodeName + "<br>";
        here = document.getElementsByTagName("p");
        here.innerHTML(string);
    }
}

Side note: In ES6, when it arrives, if you want you can use let and scope i to the for statement only. But for now, use var.

0
0

ERROR Message

ReferenceError: assignment to undeclared variable "x" (Firefox)
ReferenceError: "x" is not defined (Chrome)
ReferenceError: Variable undefined in strict mode (Edge)

Invalid cases
In this case, the variable "bar" is an undeclared variable.

function foo() { 
  'use strict'; 
  bar = true; 
} 
foo(); // ReferenceError: assignment to undeclared variable bar

Valid cases
To make "bar" a declared variable, you can add the var keyword in front of it.

function foo() {
  'use strict';
  var bar = true;
}
foo();
1
  • What if you want bar to be available in the global scope? Then var wouldn't work.
    – bart
    Nov 14, 2018 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.