/ Published in: JavaScript
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.
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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);