Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing page elements

Status
Not open for further replies.

chessbot

Programmer
Mar 14, 2004
1,524
US
Does anyone know which of the following is preferable for accessing an element, [tt]anElem[/tt], inside a form, [tt]aForm[/tt]?
Options (there may be more):
Code:
document.aForm.anElem;
document.forms['aForm'].elements['anElem'];
document.getElementById('anElem');

I believe that the last two are more cross-browser compatible than the first one, but I am not sure. I would lean toward the second one for form elements and the third one for anything else (but I'm not sure why). Does anyone know anything more about this?

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 

The last two are both compatible and correct. I woud never choose the first.

Choosing between the second two is personal preference - it depends if you're using NAMEs or IDs for your form elements, which in turn depends on whether or not you need the NAMEs submitted server-side or not.

Personally, I will always use this:

Code:
document.forms['aForm'].elements['anElem']

Hope this helps,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top