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:
In the index.HTML file we link the script file using the tag. But nowhere we tell the main.js which document to access when we reference document. What tells the javascript engine which HTML file to look for?
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.
Thanks,
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:
document.querySelector("html");
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
A simple function that would increase the image by clicking and then decrease it back by clicking. But all this can be done in CSS… While nothing is clear why this is all But it’s already interesting.
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[0].style.color = 'red'; // works ok
firstListItems[1].innerHTML = 'Something else'; // works ok
let newListElement = document.createElement('li');
newListElement.innerHTML = 'Fifth item';
firstListItems.parentNode.appendChild(newListElement); // not working
Hi everyone just wanted to mention that “innerHTML” has been used quite a few times over the last few lessons but, MDN have the following message about it with regards to innerHTML and textContent: Differences from innerHTML Element.innerHTML returns HTML, as its name indicates. Sometimes people use innerHTML to retrieve or write text inside an element, but textContent has better performance because its value is not parsed as HTML.
Moreover, using textContent can prevent XSS attacks.
Just a heads up hope it helps.
Happy Days
Big Shuff