Skip to content

Instantly share code, notes, and snippets.

@Tomalak
Last active February 24, 2021 20:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tomalak/818a78a226a0738eaade to your computer and use it in GitHub Desktop.
Save Tomalak/818a78a226a0738eaade to your computer and use it in GitHub Desktop.
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
Copy link

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