Hi Simon,

Direct access will always be faster, here's how the methods work:

getElementById has to recurse through every child element (the approach of
the recursion may vary from browser-to-browser) until it finds an element
that matches the id and then it breaks out of the loop and returns the
element.

Directly accessing the elements using dot notation is less flexible
because it will only work with a specific html structure, but there's no
looping so it's a O(1) complexity algorithm (ie very fast), whereas the
getElementById algorithm gets increasingly complex as the DOM gets more
complicated.

Hope this helps.

Regards,

Andrew Ingram

> Hi,
>
> http://developer.mozilla.org/en/docs/Using_Web_Standards_in_your_Web_Pages
> states:
>
> "The best and most supported practice for getting scriptable access to
> an element in an HTML page is to use document.getElementById(id). "
>
> A colleague of mine reckons such access will be much slower than
> accessing the element directly.
>
> So which is faster?
>
> document.forms.myform.elements.field1
>
> or
>
> document.getElementById(field1)
>
>
> Cheers,
>
> Simon



*******************************************************************
List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm
Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm
Help: [EMAIL PROTECTED]
*******************************************************************

Reply via email to