Concatenating html object arrays with javascript -
i'm attempting merge 2 arrays made of html objects. reason using .concat() not work me.
here's simple pen demonstrate problem: http://codepen.io/anon/pen/kieyb
note: tried searching remotely similar found nothing answered question.
i figure can ole fashion way using for-loops rather not re-invent wheel.
var x = document.getelementbyid("hello"); var items = x.getelementsbyclassname("one"); //alert(items.length); var items2 = x.getelementsbyclassname("two"); //alert(items2.length); items = items.concat(items2); //alert(items.length);
items
, items2
nodelist
or htmlcollection
objects, not arrays. not contain .concat()
method. have .length
property , support [x]
indexing, not have other array methods.
a common workaround copy them actual array follows:
// convert both arrays have full complement of array methods var array1 = array.prototype.slice.call(x.getelementsbyclassname("one"), 0); var array2 = array.prototype.slice.call(x.getelementsbyclassname("two"), 0);
Comments
Post a Comment