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 Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perform: Appending and Removing Elements

why not parentElement instead of parentNode?

this guy uses the parentNode property to get the parent of an element node. the parent will always be an element in our case (a li element) so idk why he decided to use parentNode instead of parentElement.

2 Answers

Steven Parker
Steven Parker
231,269 Points

The parent is not always an element, for example the parent of the html element.

The reason you might want to use parentElement is if you are specifically looking for a parent which IS an element. You would likely test the result for null and do something different in that case.

That is an excellent point!

Gavin Broekema
seal-mask
.a{fill-rule:evenodd;}techdegree
Gavin Broekema
Full Stack JavaScript Techdegree Student 22,443 Points

But in this case the parent is in fact an element. So why not just use parentElement for readability purposes? Not trying to challenge you lol, just curious.

So I had to look this up as I had no clue. Here is a stack overflow question that goes over it pretty well http://stackoverflow.com/questions/8685739/difference-between-dom-parentnode-and-parentelement . Parent element would work fine, as long as what you are looking for is an element. parentNode will work in all cases.

Question is why even create parentElement?

i read that too. i was just wondering if there was a reason why i would use parentElement. my conclusion: parentElement is useless and parentNode is the one i should use

I am inclined to agree, the only difference is that to a reader, parentElement is more intuitive. That said this is all inside the DOM so most readers probably understand what a node is. I personally can't see a reason for using the parentElement property.