Return to Snippet

Revision: 43359
at March 22, 2011 16:20 by anagaiyahoocom


Updated Code
function makeArray(items)
{
  try {
	//this converts object into an array in non-ie browsers
        return Array.prototype.slice.call(items);
	}
	catch (ex) {
		var i	= 0,
			len     = items.length,
			result	= Array(len);

		while(i < len) {
			result[i] = items[i];
			i++;
		}

		return result;
		
	}	
}

var inputs = document.getElementsByTagName('input');

var inp = makeArray(inputs);

Revision: 43358
at March 22, 2011 16:12 by anagaiyahoocom


Initial Code
function makeArray(items)
{
  try {
	//this converts object into an array in non-ie browsers
        return Array.prototype.slice.call(items);
	}
	catch (ex) {
		var i	= 0,
			len     = items.length,
			result	= Array(len);

		while(i < len) {
			result[i] = items[i];
			i++;
		}

		return result;
		
	}	
}

Initial URL


Initial Description
Some HTMLCollection objects such as returned from document.getElementsByTagName() look like arrays but are not. You will need to run the object through slice.call() to convert to an actual array. 

This function will convert array like objects into a real array. IE does not support slice.call(). The try-catch will genereate an exception and convert object to an array with a loop for IE.

Initial Title
Convert Array Like Object to an Array

Initial Tags


Initial Language
JavaScript