var b = document.getElementsByName("button");
var l = document.getElementsByName("link");
Which is the best way to add the content of these two arrays into one? I don't want to use for loop
var b = document.getElementsByName("button");
var l = document.getElementsByName("link");
Which is the best way to add the content of these two arrays into one? I don't want to use for loop
You can use Array.prototype.slice.call()
to turn the nodeLists
into real arrays and then use array operations on them to combine them
var b = document.getElementsByName("button");
var l = document.getElementsByName("link");
// make both nodeLists into real arrays
var copyB = Array.prototype.slice.call(b, 0);
var copyL = Array.prototype.slice.call(l, 0);
var combined = copyB.concat(copyL);
Working demo: http://jsfiddle.net/jfriend00/vZ5tb/
Or, a little briefer version:
var copyB = Array.prototype.slice.call(document.getElementsByName("button"), 0);
var copyL = Array.prototype.slice.call(document.getElementsByName("link"), 0);
var combined = copyB.concat(copyL);