I'm trying to determine if an element is a descendant of another elect that has a specific attribute. So far, I have no issue when the value is true, but I get TypeError: undefined is not a function (evaluating 'node.hasAttribute(attribute)')
when hasAttribute is false. Any ideas?
document.body.addEventListener('touchmove', function(event) { stopScroll(event); });
var stopScroll = function(event) {
var target = event.target;
var parent = target.parentNode;
if (isDescendantOf.attribute('data-scroll', target)) {
} else {
event.preventDefault();
}
};
var isDescendantOf = {};
isDescendantOf.parent = function(parent, child) {
var node = child.parentNode;
while (typeof node !== 'undefined') {
if (node == parent) {
return true;
}
node = node.parentNode;
}
return false;
};
isDescendantOf.tag = function(tag, child) {
var node = child.parentNode;
while (typeof node !== 'undefined') {
if (node.tagName == tag) {
return true;
}
node = node.parentNode;
}
return false;
};
isDescendantOf.attribute = function(attribute, child) {
var node = child.parentNode;
while (node !== null) {
console.log(typeof node);
if (node.hasAttribute(attribute) === true) {
return true;
}
node = node.parentNode;
}
return false;
};
Here's the fiddle, though it acts even weirder for some reason: http://jsfiddle.net/x95tgmkn/