Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Leo Marco Corpuz18,975 Points
Traversing the DOM practice. Using "Element"
On the Traversing the DOM practice, is it ok to leave out "Element" with DOM relations (example previousSibling instead of previousElementSibling)?
James Nakolah10,542 Points
previousElementSiblingis an improvement of
What I mean here is while
previousSibling may in most cases work out just fine depending on how you've structured your html, it is important to understand that
previousSibling points to the previous node in the dom structure. The problem will come when you have an empty text node or some other node type between your elements, this will definitely lead to unexpected behaviour.
previousElementSibling on the other hand ensures it's an element and leaves out other node types.