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

ParentNode is a raw interface and no object of this type can be created; it is implemented by Element, Document, and Doc

Trying to understand MDN docs. Came across this. Is it saying that ParentNode is a built in object/method that we have access to but we can't make our own?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I don't believe so, no. But I'm not sure why exactly you'd want to either. However, you can access the ParentNode of an element and set it to a variable. I've made a small example for you to show how this might be done. The style I'm using is in the head section instead of making a completely new file.

<!DOCTYPE html>
<html>
    <head>
       <title>Test</title>
        <style>
            .myClass {
                background-color: tomato;
            }
        </style>
    </head>
    <body>
        <div id="listDiv">
           <div class ="content">
                <h1>This is my content div!</h1>
           </div>
            <div id="output">
                <ul id="myList">
                    <li>Item #1</li>
                    <li>Item #2</li>
                    <li>Item #3</li>
                </ul>
            </div>
        </div>
         <script src="testjs.js"></script> 
    </body>
</html>

And here's the JavaScript:

var myParentNode = document.getElementById("myList").parentNode;
console.log(myParentNode);
myParentNode.className ="myClass";

The console will log out the div with the id of "output" as it is the Parent node of the ul with the id "myList". I then take that variable name and assign the class to it which results in a "tomato" background color. Hope this helps! :sparkles:

Thank you Jennifer, that is a beautifully illustrated example.