Last active
May 7, 2016 14:10
-
-
Save englishextra/6d843e16c49cfde0a394bd9d76383b13 to your computer and use it in GitHub Desktop.
get children of a parent element by tag name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* 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