There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
The DOM tree is: document -> html -> body/head per the image at the start of this section.
We have document.body and document.head, but is there a way to reference document.html, or is that functionally equivalent to just document? Curious since if html is part of the DOM tree, there doesn’t seem to be a way to access it.
What is linking the two files i.e. index.html and main.js together?
In the exercise, we learn that document keyword gives access to the root node and from there on one can access various elements. I have a very basic doubt:
An addendum to that question:
If there were two HTML documents say index.HTML and appendix.HTML and both included a script tag referring to a single js file say utils.js. This file may have some utility functions to add interactivity or some other frequently used functions. How would the utils.js figure out which file to reference using Document keyword.
The html element isn’t the root element - in fact, it’s a child of the root element. To access the html element, you can use the .querySelector() method, like the following:
The script will run in the HTML document it is loaded in. So if we loaded it in index.html, it will be executed in index.html meaning the document object refers to the root node of index.html. If we loaded this script in two HTML documents, it will be equal to both! This might seem confusing, but whatever page you are on, the document will be equal to the root node of that document
Can someone explain to me why the following doesn’t work?
I’m trying to modify the first list (under the heading ‘Species’). When I try to create another list item and append it to the parent node of the ‘li’ items (which is ‘ul’), it doesn’t appear. Thanks!
let firstListItems = document.getElementsByTagName('li');
firstListItems.style.color = 'red'; // works ok
firstListItems.innerHTML = 'Something else'; // works ok
let newListElement = document.createElement('li');
newListElement.innerHTML = 'Fifth item';
firstListItems.parentNode.appendChild(newListElement); // not working