Skip to content Skip to sidebar Skip to footer

In Javascript Is This A Valid Construct?: Document.name.name.value?

Here's the form:

Solution 1:

UPDATE

According to WHATWG 6.2.4 Named access on the Window object

The Window object supports named properties. The supported property names of a Window object window at any moment consist of the following,...for all applet, embed, form, frameset, img, and object elements...


According to W3C DOM 2 HTML Specification 2.7.2.1 HTMLAllCollection

The following elements name attribute can be referenced as a property of the document object: anchor, applet, button, form, frame, iframe, img, input, map, meta, object, param, select, and textarea


This referencing approach is standard, but it's use is generally discouraged. A few reasons to avoid directly referencing DOM property or window object by name attributes are: variable shadowing, inadvertently scoping to the window object, major browser inconsistencies, etc. For details on why it should be avoided, read this section and this post.

This Snippet shows a stable and standard way of using form names as a reference document.forms and the referencing form names previously mentioned as well.

SNIPPET

var val1 = document.forms.fname.elements.iname.value;

console.log(val1);

var val2 = fname.iname.value;

console.log(val2);
<form name='fname'>
   <input name='iname' value="42">
</form>

Post a Comment for "In Javascript Is This A Valid Construct?: Document.name.name.value?"