HighDots Forums  

document.forms[formName].elemName

Javascript JavaScript language (comp.lang.javascript)


Discuss document.forms[formName].elemName in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Stanimir Stamenkov
 
Posts: n/a

Default document.forms[formName].elemName - 05-25-2008 , 07:12 AM






<form name="myForm" action="...">
<p><input type="text" name="myElem"></p>
</form>

As far as I was able to get the following is the standard way of
accessing HTML form elements:

document.forms['myForm'].elements['myElem']

But I have also seen the following:

document.forms['myForm'].myElem

Is the later correct usage or it just happens all browsers I've
tried with (IE 6, Mozilla 1.8, Opera 9.2 and Safari 3.1 on Windows)
support it for compatibility with existing Web content?

--
Stanimir

Reply With Quote
  #2  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: document.forms[formName].elemName - 05-25-2008 , 10:29 AM






Stanimir Stamenkov wrote:
Quote:
form name="myForm" action="..."
p><input type="text" name="myElem"></p
A `div' element instead of `p' would be semantic here. It isn't exactly a
text paragraph, is it?

Quote:
/form

As far as I was able to get the following is the standard way of
accessing HTML form elements:

document.forms['myForm'].elements['myElem']
Correct, as far as the standard goes. That the object referred to by
`document' implements the HTMLDocument interface of W3C DOM Level 2 HTML
in many cases has been a proprietary, yet reasonable design decision.

Quote:
But I have also seen the following:

document.forms['myForm'].myElem

Is the later correct usage
I think it qualifies as deprecated usage by now.

http://docs.sun.com/source/816-6408-10/form.htm

Quote:
or it just happens all browsers I've
tried with (IE 6, Mozilla 1.8, Opera 9.2 and Safari 3.1 on Windows)
support it for compatibility with existing Web content?
It would seem so.

http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-40002357
http://www.w3.org/TR/DOM-Level-2-HTML/glossary.html


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7 (AT) news (DOT) demon.co.uk>


Reply With Quote
  #3  
Old   
VK
 
Posts: n/a

Default Re: document.forms[formName].elemName - 05-25-2008 , 10:51 AM



On May 25, 3:12 pm, Stanimir Stamenkov <s7a... (AT) netscape (DOT) net> wrote:
Quote:
form name="myForm" action="..."
p><input type="text" name="myElem"></p
/form

As far as I was able to get the following is the standard way of
accessing HTML form elements:

document.forms['myForm'].elements['myElem']

But I have also seen the following:

document.forms['myForm'].myElem
The latter is a shortcut accessor to the same element. Most of the
time the shortcut form can be used to preserve your keyboard and your
finger tips :-) It is not the case when the form control name doesn't
conform with Javascript valid identifier rules. Imagine you have a set
of radioboxes with names adjusted for PHP server-side pre-processing,
so something like <input type="radio" name="radio[0]">, "radio[1]"
etc. Obviously by using the shortcut form
document.forms['myForm'].radio[0] you'll get runtime errors. At the
same time the fully qualified notation will work:
document.forms['myForm'].elements['radio[0]']




Reply With Quote
  #4  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: document.forms[formName].elemName - 05-25-2008 , 11:01 AM



VK wrote:
Quote:
On May 25, 3:12 pm, Stanimir Stamenkov <s7a... (AT) netscape (DOT) net> wrote:
form name="myForm" action="..."> <p><input type="text"
name="myElem"></p> </form

As far as I was able to get the following is the standard way of
accessing HTML form elements:

document.forms['myForm'].elements['myElem']

But I have also seen the following:

document.forms['myForm'].myElem

The latter is a shortcut accessor to the same element.
It sa reference to the same element _object_, if that.

Quote:
Most of the time the shortcut form can be used to preserve your keyboard
and your finger tips :-) It is not the case when the form control name
doesn't conform with Javascript valid identifier rules.
Wrong. The proprietary referencing allows

document.forms['myForm']["myElem[]"]

as well.

The reason for using the standards-compliant approach over the proprietary
one is that the latter is the *proprietary* one. Proprietary approaches
should be avoided, and should only serve as a fallback for
standards-compliant approaches because, theoretically, by definition they
could break any minute the code is exposed to another, previously unknown,
user agent.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee


Reply With Quote
  #5  
Old   
Stanimir Stamenkov
 
Posts: n/a

Default Re: document.forms[formName].elemName - 05-25-2008 , 11:05 AM



Sun, 25 May 2008 07:51:22 -0700 (PDT), /VK/:
Quote:
On May 25, 3:12 pm, Stanimir Stamenkov <s7a... (AT) netscape (DOT) net> wrote:

document.forms['myForm'].elements['myElem']

But I have also seen the following:

document.forms['myForm'].myElem

The latter is a shortcut accessor to the same element. Most of the
time the shortcut form can be used to preserve your keyboard and your
finger tips :-)
Yes, it seems to save typing but I haven't found it described in the
ECMAScript language binding [1] of the DOM Level 2 HTML
specification, so I've thought it is just deprecated as Thomas Lahn
suggested in another reply. Having said that my question could be
stated better as "should the second form of access not be used in
newly written scripts?".

[1] http://www.w3.org/TR/DOM-Level-2-HTM...t-binding.html

--
Stanimir


Reply With Quote
  #6  
Old   
VK
 
Posts: n/a

Default Re: document.forms[formName].elemName - 05-25-2008 , 11:18 AM



On May 25, 7:01 pm, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de>
wrote:
Quote:
The reason for using the standards-compliant approach over the proprietary
one is that the latter is the *proprietary* one.
???

forms[index] or forms["formName"] exposes an object with properties
representing form elements (a.k.a. controls) in the given form.
Javascript allows to access object property over squared brackets
notation or over dot notation, other words in object foo with property
bar, the bar value can be accessed either
foo["bar"]
or
foo.bar
The latter form is a convenience shortcut of the first one and
acceptable iff the property name corresponds to the Javascript
identifier naming rules. Say if the property named not "bar" but
"class" or "bar[0]" or "#$&%&#*" etc. then only the full form is
usable.

These are basics of the language itself, so naturally they are not
documented over and over again wherever objects are discussed: just
like in a complex analysis math books they don't explain what do +, -
and other basic signs mean before each new formula. Once explained -
works everywhere further unless explicitly spelled otherwise.


Reply With Quote
  #7  
Old   
VK
 
Posts: n/a

Default Re: document.forms[formName].elemName - 05-25-2008 , 11:37 AM



On May 25, 7:05 pm, Stanimir Stamenkov <s7a... (AT) netscape (DOT) net> wrote:
Quote:
Yes, it seems to save typing but I haven't found it described in the
ECMAScript language binding [1]
See my answer to Thomas. Both accessor syntax types are in the
language core, so not explained in each particular application.


Reply With Quote
  #8  
Old   
Stanimir Stamenkov
 
Posts: n/a

Default Re: document.forms[formName].elemName - 05-25-2008 , 12:02 PM



Sun, 25 May 2008 08:37:30 -0700 (PDT), /VK/:
Quote:
On May 25, 7:05 pm, Stanimir Stamenkov <s7a... (AT) netscape (DOT) net> wrote:

Yes, it seems to save typing but I haven't found it described in the
ECMAScript language binding [1]

See my answer to Thomas. Both accessor syntax types are in the
language core, so not explained in each particular application.
I understand if object.propertyName works, object["propertyName"]
will also work equally but could you point me where in the DOM Level
2 HTML specification (and its ECMAScript language binding) it is
stated form elements are exposed as properties of the
HTMLFormElement object?

While these are same:

document.forms["formName"].elemName
document.forms["formName"]["elemName"]

they are different from:

document.forms["formName"].elements["elemName"]

the later being the only thing I've found defined in the standard.

--
Stanimir


Reply With Quote
  #9  
Old   
Steve
 
Posts: n/a

Default Re: document.forms[formName].elemName - 05-25-2008 , 12:10 PM



On May 25, 5:01 pm, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de>
wrote:

Quote:
The reason for using the standards-compliant approach over the proprietary
one is that the latter is the *proprietary* one.
Could you explain what this is supposed to mean?


Reply With Quote
  #10  
Old   
VK
 
Posts: n/a

Default Re: document.forms[formName].elemName - 05-25-2008 , 12:22 PM



On May 25, 8:02 pm, Stanimir Stamenkov <s7a... (AT) netscape (DOT) net> wrote:
Quote:
Sun, 25 May 2008 08:37:30 -0700 (PDT), /VK/:

On May 25, 7:05 pm, Stanimir Stamenkov <s7a... (AT) netscape (DOT) net> wrote:

Yes, it seems to save typing but I haven't found it described in the
ECMAScript language binding [1]

See my answer to Thomas. Both accessor syntax types are in the
language core, so not explained in each particular application.

I understand if object.propertyName works, object["propertyName"]
will also work equally but could you point me where in the DOM Level
2 HTML specification (and its ECMAScript language binding) it is
stated form elements are exposed as properties of the
HTMLFormElement object?
I have no a slightest clue about it, sorry - I never read any of DOM
Level 1 or 2 specs.

In any case document.forms, document.forms.elements, document.links,
document.anchors etc. are parts of the original Netscape Navigator
document model (so-called DOM 0) so out of any jurisdiction of W3C.
They simply are, always were and always will.

So if your question is carrying out some practical issue then the
answer is above. If it is an academical study then you are in some
troubles because off my head I have no idea where you could find now
any authoritative, "stamped and sealed" proof that there is indeed
window object with say setTimeout and clearTimeout methods, DOM 0,
document.forms[0].foobar-type accesor and many other things that
simply are from the beginning of times so no one ever needed to
document it.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.