Skip to content

Instantly share code, notes, and snippets.

@englishextra
Last active May 7, 2016 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save englishextra/6d843e16c49cfde0a394bd9d76383b13 to your computer and use it in GitHub Desktop.
Save englishextra/6d843e16c49cfde0a394bd9d76383b13 to your computer and use it in GitHub Desktop.
get children of a parent element by tag name
/*!
* get children of a parent element by tag name
* no fallback to document if parent fail
* gist.github.com/englishextra/6d843e16c49cfde0a394bd9d76383b13
* jsfiddle.net/englishextra/c2vr5gxd/
* jsbin.com/xavocu/edit?html,js,output
*/
getChildrenByTag = (function (d, m) {
function g(t, p, o) {
o = Object.create(g.fn);
/*!
* no fallback to document if parent fail
*/
/* var f = (function (t, p) {return p ? p[m](t) : []}(t, p)); */
/*!
* this will fallback to document if parent fail
*/
var f = (function (t, p) {if(null===p||"undefined"===typeof p)p=document||[];return p[m](t)||[];}(t, p));
if (t) {
o.push.apply(o, f);
}
return o;
}
g.fn = [];
g.one = function (t, p) {
return g(t, p)[0] || null;
};
return g;
})(document, "getElementsByTagName");
var a = getChildrenByTag("p",document.querySelector("body > div"));
var b = getChildrenByTag.one("p",document.querySelector("body > div"));
alert(a.length);
/*!
* what kinds of data can be considered empty:
* undefined or null
* a zero-length string
* an array with no members
* an object with no enumerable properties
*/
alert(b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment