Instantly share code, notes, and snippets.

Embed
What would you like to do?
A function to test if a JavaScript object is a DOM NodeList. This is designed to work across all browser implementations. StackOverflow question reference http://stackoverflow.com/a/7238344/18771.
/* Released under the MIT License in 2014. http://opensource.org/licenses/mit-license */
function isNodeList(nodes) {
var stringRepr = Object.prototype.toString.call(nodes);
return typeof nodes === 'object' &&
/^\[object (HTMLCollection|NodeList|Object)\]$/.test(stringRepr) &&
nodes.hasOwnProperty('length') &&
(nodes.length === 0 || (typeof nodes[0] === "object" && nodes[0].nodeType > 0));
}
@julianshapiro

This comment has been minimized.

Show comment
Hide comment
@julianshapiro

julianshapiro May 19, 2014

You'll want to change nodes.hasOwnProperty('length') to nodes.length !== undefined for IE8 support.

julianshapiro commented May 19, 2014

You'll want to change nodes.hasOwnProperty('length') to nodes.length !== undefined for IE8 support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment