I’m trying to iterate “up” through the DOM nodes from a given element to get the first parent element which has the attribute ‘animated’.
var el = evt.target;
console.log(el);
while (!el.hasAttribute('animated'))
{ el = el.parentNode; }
return el;
console.log(el);
Throws error:
>>>Uncaught TypeError: Object #<HTMLDocument> has no method 'hasAttribute'
How is this possible? I’ve clearly declared the variable el and the first log is correct .
The
documentobject:parentNodeof the root element (if you were using HTML that would be the<html>element)Only elements have attributes, so only element objects have a
hasAttributemethod.You need to stop testing when you reach the document object (or when you aren’t testing an element any longer).