Initially, I was seeking a sophisticated method to replicate the functionality of Array.concat()
on the results of the getElementsByTagName
function in older browsers like IE because it appeared that concat
wasn't supported. However, as it turns out, the reason the returned object didn't support it is because it's not actually an Array
. Whoops!
The truth is, getElementsByTagName
returns a NodeList
. So, the real question becomes: what's the best approach to obtain a single list containing all form elements in a document (input, select, textarea, button) for looping through them? It doesn't necessarily have to be an array... a single NodeList
would also work perfectly.
Please keep in mind that I'm working with IE6 for a corporate intranet system (soon upgrading to IE8).
My solution eventually ended up being:
It turned out to be more straightforward and likely more efficient to encapsulate the code into a separate function and call it three times with the different nodelists rather than attempting to merge them together.
In the end, I switched over to using MooTools (after spending hours researching various frameworks). Now, obtaining an array of desired items is incredibly simple. I suggest utilizing a JavaScript framework like this instead of struggling to find the optimal approach yourself. Of course, I value learning the raw language (which is why I hesitated to use a framework for so long), but sometimes, the quickest way to achieve results is just as important in a business setting as improving one's proficiency with the language.
Update: Nearly two years later, I'd simply opt for jQuery and call it a day!