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


i have javascript function accepts list of html nodes, expects javascript array (it runs array methods on that) , want feed output of document.getelementsbytagname returns dom node list.

initially thought of using simple like:

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

and works fine in browsers, except of course internet explorer returns error "jscript object expected", apparently dom node list returned document.getelement* methods not jscript object enough target of function call.

caveats: don't mind writing internet explorer specific code, i'm not allowed use javascript libraries such jquery because i'm writing widget embedded 3rd party web site, , cannot load external libraries create conflict clients.

my last ditch effort iterate on dom node list , create array myself, there nicer way that?

nodelists host objects, using array.prototype.slice method on host objects not guaranteed work, ecmascript specification states:

whether slice function can applied host object implementation-dependent.

i recommend make simple function iterate on nodelist , add each existing element array:

function toarray(obj) {   var array = [];   // iterate backwards ensuring length uint32   (var = obj.length >>> 0; i--;) {      array[i] = obj[i];   }   return array; } 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -