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">
</form>


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>
</div>


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.

http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-1689064

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