BadCash BadCash -3 years ago 102
HTML Question

Why can only forms be referenced by dot notation?

It seems like a simple beginners question, but I'm unable to find an answer anywhere.

Let's say I have this HTML:

<form name="myForm">
<input type="text" name="myTxt">

then I can use the following javascript

document.myForm.myTxt.value = 'foo';

But what if I have a div instead of a form?

<div name="myDiv">
<span name="mySpan"></span>

Why can't I do the same thing here, like

document.myDiv.mySpan.innerHTML = "bar";

Seems like it should be possible to do, instead of having to use getELementById(), but I can't make it work.

Answer Source

As designed by W3C.

There's really nothing to argue about. You can access the an HTMLCollection of all the forms on the page through document.forms, much like you can images with document.images, applets with document.applets, links with document.links and anchors with document.anchors.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download