tags:

views:

27

answers:

1

Hi Guys,

I would like to know which of the following method is better?

document.getElementById or documennt.forms[]?

Which is the one that cshould be used regularly.
The focus is on performance.

Regards,
Naveen

+2  A: 

I would use document.getElementById, you can be consistent for getting everything, not just forms by ID...this works for any and all elements, and it's the fastest way to select an object to boot.

I'm not sure about IE, but you can think of browsers as having a hashtable implementation mapping ID to element (since they have to be unique, or so says the spec, so the browser is free to assume that they are). This is what makes that selection so fast, and also what makes the action only work on the first element with an ID if you violated the spec and re-used it :)

Nick Craver