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 trialsibal gesekki
3,484 Pointswhy 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
231,198 PointsThe 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.
James Bradd
4,597 PointsSo 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?
sibal gesekki
3,484 Pointsi 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
James Bradd
4,597 PointsI 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.
James Bradd
4,597 PointsJames Bradd
4,597 PointsThat is an excellent point!
Gavin Broekema
Full Stack JavaScript Techdegree Student 22,443 PointsGavin Broekema
Full Stack JavaScript Techdegree Student 22,443 PointsBut 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.