dagblog.
← Archive

The forms collection and the fieldset element


The forms collection and the fieldset element

In the fourth edition of JavaScript: The Definitive Guide, David Flanagan points out
some interesting browser behavior. The HTML 4.01 recommendation added
the fieldset element. There’s nothing especially unusual about that.
The odd part is what browsers do when a fieldset occurs
inside a form element. The size of the forms collection exceeds
the number of form controls by the number of nested fieldset elements. See the example.

Calling document.forms["foo"].length returns
2, although it should logically return 1 because the form
has only one control. If your scripts
use element indexes or assume a fixed number
of form controls, remember that adding a fieldset
can make them behave incorrectly. Internet Explorer started adding elements to the forms collection
in a nonstandard way by including fieldset. Mozilla and Opera had to follow suit for script compatibility.

Medium version

Related articles

Related CZ Podcast episodes