SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Which is faster: document.getElementById or document.myform.myelement ?

    Hi folks,

    Assuming a form element has the same ID and NAME, which is faster: document.getElementById or document.myform.myelement?

    I always assumed the latter since it's a directly path to the element, but I thought I'd probably better ask.
    A.P.

    generatedata.com - free JS/PHP/MySQL random test data generator.
    Form Tools - free PHP/MySQL form processor.

  2. #2
    SitePoint Addict richtestani's Avatar
    Join Date
    Nov 2003
    Location
    Bridgeport
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The difference I don't believe is speed, but how your elements are accessed.

    getElementById accesses the element by identifier name, where as the latter is accessed using the .formName.elName notation.

    For standards based programing the first method is preferred - its also easier to type.

    Someone else can probably give you a more definitive answer.
    Last edited by richtestani; May 18, 2007 at 12:17. Reason: misspelling
    RichTestani
    -------------------------------
    http://www.junkdepot.com
    http://www.rareoopdvds.com | The Movie Poster Site

  3. #3
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    NY, USA
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you mean "faster"?

    It's just 2 ways of referencing a web page element.

  4. #4
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AtomicPenguin View Post
    I always assumed the latter since it's a directly path to the element, but I thought I'd probably better ask.
    Why don't you try it out yourself? You can find an easy to use benchmarking method here. Just make two functions, one which uses document.getElementById, and one which uses document.myform.myelement, then benchmark them using the code linked to above.

    You might find that both methods are so similar, that you won't be able to tell them apart. In which case, you won't need to worry about their relative speed.

    Douglas
    Hello World

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    don't forget document.getElementsByName('myelementname')[0];

    Rock beats scissors beats paper beats rock.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Third Stone From The Sun
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    getElementById is rock.
    My outdated site is down for a while now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •