findup-element
Version:
Given a child element, climb up the DOM and find the first parent element matching your criteria
29 lines (21 loc) • 488 B
JavaScript
module.exports = findup
function findup(child, check) {
if (typeof check === 'string') check = byName(check)
if (typeof check !== 'function') check = byExact(check)
while (
child &&
!check(child)
) child = child.parentNode
return child || null
}
function byName(name) {
name = String(name).toUpperCase()
return function(element) {
return name === element.nodeName
}
}
function byExact(el) {
return function(element) {
return el === element
}
}