How to convert a DOM node list to an array in Javascript?

Posted by Guss on Stack Overflow See other posts from Stack Overflow or by Guss
Published on 2010-04-29T05:59:39Z Indexed on 2010/04/29 6:17 UTC
Read the original article Hit count: 263

Filed under:
|
|

I have a Javascript function that accepts a list of HTML nodes, but it expects a Javascript array (it runs some Array methods on that) and I want to feed it the output of Document.getElementsByTagName that returns a DOM node list.

Initially I thought of using something simple like:

Array.prototype.slice.call(list,0)

And that works fine in all browsers, except of course Internet Explorer which returns the error "JScript object expected", as apparently the DOM node list returned by Document.getElement* methods is not a JScript object enough to be the target of a function call.

Caveats: I don't mind writing Internet Explorer specific code, but I'm not allowed to use any Javascript libraries such as JQuery because I'm writing a widget to be embedded into 3rd party web site, and I cannot load external libraries that will create conflict for the clients.

My last ditch effort is to iterate over the DOM node list and create an array myself, but is there a nicer way to do that?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about dom