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! While you're at it, check out some resources Treehouse students have shared here.

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

JavaScript Introducing the Practice

Leo Marco Corpuz
Leo Marco Corpuz
18,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)?

2 Answers

previousElementSibling is an improvement of previousSibling.

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.

Doron Geyer
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points

Leo Marco Corpuz the Element bit is necessary because of how javascript structures some things. In the tutorial they showed an example where in an unordered list if you use previous element it returns an empty string instead of the previous element which would be a list item. so Adding Element just ensures that it will always return an HTML element rather than sometimes returning something you dont expect.