Is there an agreed set of \"best practices\" for use of the DOM Level 0 collections in modern JavaScript applications? (document.forms
, document.images
I'll start by saying that I don't know of any "official" statements like "DOM Level 0 considered harmful". This is just my own opinion.
I think it depends on the case.
The issue I have is that it is problematic, especially with dynamically generated/modified documents, to target specific elements using the collections like documents.forms
or documents.images
. Every time I see someone write document.forms[0]
, I cringe.
Current, and even not-so-current browsers provide the ability via functions like getElementsByTagName
to simulate the same behaviour, but in a more generic and reusable way. Of course, when you add jQuery into the mix, then there's no reason at all to use those collections.
The exceptions I have though, are when you are working in a non-jQuery environment and you're accessing things like selectElement.options
or tbodyElement.rows
. In those cases, it really is simpler when you're doing basic stuff like adding or removing items.