|
-
Feb 8th, 2008, 11:39 AM
#1
[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.
-
Feb 8th, 2008, 12:04 PM
#2
Addicted Member
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
-
Feb 8th, 2008, 12:37 PM
#3
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
-
Feb 9th, 2008, 03:38 AM
#4
Re: document.getElementById vs document.forms
 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.
-
Feb 9th, 2008, 06:16 AM
#5
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.
-
Feb 11th, 2008, 09:29 AM
#6
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
-
Feb 11th, 2008, 09:47 AM
#7
Re: document.getElementById vs document.forms
Thanks everyone for the clarification.
-
Feb 11th, 2008, 10:03 AM
#8
Re: document.getElementById vs document.forms
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|