Nathan Jones
Nathan Jones

Reputation: 5144

Why is JSLint giving me an "Unexpected '.'" error with this code?

JSLint is giving me an "Unexpected '.'" error with this code on line 2 (with the for loop), and it stops scanning the file at this point as well.

var form = document.form1;
for (var i=0; i < form.elements.length; i++) {
  // inner-loop code not included here
}

I checked in the Developer Console in Chrome, and form.elements does contains what I'd expect. Why is JSLint giving me this error?

I'm using JSLint through PhpStorm.

Upvotes: 1

Views: 404

Answers (2)

frederickf
frederickf

Reputation: 1531

We need more information to really answer this. The HTML and the all the JS. If it's too long to post in the body of the question you could try hosting it at a service like http://jsfiddle.net/

Without being able to reproduce it I'm going to guess that document.form1 isn't what you want. Perhaps you meant document.forms. That gives access to all the forms in the DOM as an htmlCollection, which you access like an array. For example document.forms[1]. Read more at https://developer.mozilla.org/en-US/docs/Web/API/document.forms

Upvotes: 1

robbmj
robbmj

Reputation: 16506

I am unable to reproduce your issue in JSLint. However, since JS does not have block scope and instead function scope, JSLint (Doug Crockford) recomends all variable declaration happen at the top of the function.

Also he recommends not using the ++ operator and instead to += 1. Try the below code in JSLint and make sure "assume browser" is set to true

function foo() {
    'use strict';

    var form = document.form1,
        i;
    for (i = 0; i < form.elements.length; i += 1) {
        window.console.log(i);
    }
}

Upvotes: 1

Related Questions