Results 1 to 8 of 8

Thread: [RESOLVED] document.getElementById vs document.forms

  1. #1

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Resolved [RESOLVED] document.getElementById vs document.forms

    What is the difference between the two of these? I always assumed it was just a different way of getting the same object.

    Now I find myself, more often than not, having to use document.forms to get values from items such as a radio button or a text area.

    Could someone explain to me why this happens?

    E.g) On a project I am working on, I have a textarea I want to validate.

    document.getElementById("reason").value returns 'undefined'
    document.forms[0].reason.value returns the actual value


    Any feedback is appreciated.

  2. #2
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Re: document.getElementById vs document.forms

    If reason is a textarea, I think you should use .innerHTML not .value .
    If reason is input type text, then both should return the value .
    Circa 1995
    Engineer - I think we should put our website address on our paper catalogs.
    Vice President - Don't get too excited about this internet thing.


    I am sorry, but the Oracle was mistaken. You cannot help us.
    -Matrix video game


    I'm doing a (free) operating system (just a hobby, won't be big and professional like gnu) for 386(486) AT clones. ... and it probably never will support anything other than AT-harddisks, as that's all I have :-(.
    -Linus


    Question. Do you know that the character "?" means I'm asking a question? Question. Do you know that spoken inflection also provides the same cue? So please don't say, "Question" before you ask your question. Believe me I'll know.

    That said, I would have said this first if it had to precede what I'm telling you now. Having said that, what I'm telling you now is the same thing I just said about the annoying phrases "That said" and "Having said that".


    Are you threatening me, Master Jedi?
    -Chancellor Palpatine

  3. #3

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: document.getElementById vs document.forms

    Wow, .value works fine, but attempting to use .innerHTML showed me that I was a retard and had my Div above the textarea also with an ID of "reason."

    My question still stands though, since I have ran into this in many other situations where document.forms[] seems to have more functionality than document.getElementById

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: document.getElementById vs document.forms

    Quote Originally Posted by kfcSmitty
    I always assumed it was just a different way of getting the same object.
    That is all.

    document.forms is part of the DOM specification and so should not be unnecessarily avoided in favour of getElementById. Use whatever is easier.

    The innerHTML property, on the other hand, should be treated with caution: it won't work with XML documents.

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: document.getElementById vs document.forms

    As a note, Firefox 1.0 didn't support innerHTML with XHTML documents. They changed that after a flood of feedback. I guess that'll ensure that XHTML 1.0 pages will always have a support for innerHTML despite that being incorrect from the standards perspective. XHTML 1.0 has ended up being handled as text/html anyway - personally I don't see a major problem in this, like some evangelist purists do. Although what I agree with is that XHTML 1.1 should always be handled as application/xhtml+xml, effectively making it unusable in current web. And innerHTML shouldn't be used on those pages, if one for some reason wants to make a page in XHTML 1.1 and totally ignore IE users.

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: document.getElementById vs document.forms

    document.getElementById() is the recommended way to get reference of elements in your web page. This is easier to use and also would not require any changes if you decide to change something else like the Form name (or Id) etc.
    However, as it traverses the entire DOM tree, it is usually slower (not noticeable) than the document.forms notation of referencing objects.

    document.forms is harder to use in the sense that you need you to know properly the entire path to the object beginning from the document element. But on the other hand it is comparatively faster (not noticeable).

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: document.getElementById vs document.forms

    Thanks everyone for the clarification.

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: document.getElementById vs document.forms

    Quote Originally Posted by Pradeep1210
    document.getElementById() is the recommended way to get reference of elements in your web page. This is easier to use and also would not require any changes if you decide to change something else like the Form name (or Id) etc.
    However, as it traverses the entire DOM tree, it is usually slower (not noticeable) than the document.forms notation of referencing objects.

    document.forms is harder to use in the sense that you need you to know properly the entire path to the object beginning from the document element. But on the other hand it is comparatively faster (not noticeable).

    Bollocks.

Posting Permissions

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



Click Here to Expand Forum to Full Width