...

View Full Version : concat() method returning "not a function"



]|V|[agnus
01-19-2005, 06:02 PM
If I get a collection of elements using the getElementsByTagName() method, and then get a second collection using the same method, shouldn't I be able to merge them using the concat() method?

Example:


var d = document;
var inputs = d.getElementsByTagName("input");
var textareas = d.getElementsByTagName("textarea");
var everything = inputs.concat(textareas);


I am wanting to do something similar to the above to apply a common focus()/blur() behavior to these form elements, my thinking being that merging the collections before looping over them might lessen some overhead. But I keep getting "inputs.concat is not a function" from the Firefox JS console.

fci
01-19-2005, 06:10 PM
concat only works for strings and arrays.

Kor
01-19-2005, 06:11 PM
I have encountered the same problem, take a look, it might help you:
http://www.codingforums.com/showthread.php?t=47683

]|V|[agnus
01-19-2005, 06:17 PM
fci:
I thought getElementsByTagName() returned at array.

Kor:
Thanks for the quick response and handy link!

fci
01-19-2005, 06:23 PM
use typeof to find out what type a variable is. for example:

alert(typeof inputs);

]|V|[agnus
01-19-2005, 06:34 PM
Good tip, fci, thanks.

Kor,
I see you found a sort of workaround for your methods. How might I take two of these collections and create one, single-dimension array? So that, say, everything[0] through everything[10] are <input /> elements, and then everything[11] starts with the <textareas />?

fci
01-19-2005, 06:52 PM
var d = document;
var inputs = d.getElementsByTagName("input");
var textareas = d.getElementsByTagName("textarea");
function concat_collection(obj1, obj2) {
var i;
var arr = new Array();
var len1 = obj1.length;
var len2 = obj2.length;
for (i=0; i<len1; i++) {
arr.push(obj1[i]);
}
for (i=0; i<len2; i++) {
arr.push(obj2[i]);
}
return arr;
}
var blah = concat_collection(inputs,textareas);

it has a length property similar to an array.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum