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!
If you look at the index.html it will make sense because they want you to use the DOM. The id=more-destinations comes before the id=oaxaca, so that’s why they want you to start from the parent the go down; however, I reversed the two and it still works. Creating variable for the parent and the child makes the code look cleaner and not too long. The last step is self explanatory
Took me awhile to figure out a solution to this exercise. After reading through this thread and also the proposed solution, it decided to try to follow the sequential steps given.
I came up with this solution… I believe it is valid, but would like to know what are the advantages of the proposed solution compared to this.
let child = document.querySelector(’#oaxaca’);
document.querySelector(’#more-destinations’).removeChild(child);
Now only the parent is still referable in memory, and will remain so, as a constant. There are no references to the removed element. We can add to and remove elements from the parent as much as we like.
The # tells document.querySelector() that you are looking for an element with an id of ‘oaxaca’. If you wanted to select an element by its class instead of id, you would use . like so: document.querySelector('.title').
Yeah this one wasn’t thought out too well. I cheated and went to the ‘solution’. And got very confused when I realized they never gave an example looking anything like that lol
Since both oaxaca and more-destinations are unique ID’s within the HTML file couldn’t we target those ID’s and accomplish the same results removing the paragraph? This is what I was thinking of
l et child= document.getElementbyId(‘#oaxaca’)
let parent= document.getElementbyId(‘#more-destinations’)
parent.removeChild(child)
However I get the error below.
Did you remove the #oaxaca element from the parent #more-destinations ?
Does the same go for .removeChild(), in that, it doesn’t accept CSS Syntax??
I was attempting the following and getting the incorrect answer message:
var mexico = document.getElementById('oaxaca'); document.querySelector('#more-destinations').removeChild('#oaxaca');
I realised, I should have used the variable ‘mexico’ instead and the solution was accepted. Although, my first attempt still made sense to me as .removeChild(’#oaxaca’) was referencing an element ID.
When you use document.querySelector() to select an element by it’s ID you have to use # before it’s ID just like how you would in a CSS file.
If you don’t the browser will look an element with the given string i.e. document.querySelector("oaxaca") will search for an element <oaxaca /> whereas document.querySelector("#oaxaca") will search for an element with the id="oaxaca".
I also saw someone mentioning the order of the code line should be in a specific way. I tried it and both ways work.
I recommend using websites such as codeSandbox or similar when you get stuck because they have a console that prints errors, like in may case where I had a typo quarySelector instead of querySelector. Cought it through the console of codeSandbox.
let oaxaca = document.getElementById('oaxaca');
document.body.removeChild(oaxaca);
But I couldn’t get the expected results. The provided answer doesn’t really help, since they use two variables, whereas the provided examples use a single-line notation style.
What I’m wondering is, am I simply getting the concept wrong? could something like this work in place of my other attempt:
let parent = document.getElementById('more-destinations');
let child = document.getElementById('oaxaca');
parent.removeElement(child);
Or is this one of those weird cases where method selectors can’t be used with parentheses, and so we have to use [square brackets ] in their place?
Thoughts:
Personally, I think the official answer is rather long and inelegant to achieve something that seems rather simple. IDK, there must be a simpler way to remove a single item.
When the exercise 6, it says that .querySelector(‘p’) chooses the first paragraph it sees, so how will I be able to select other p elements other than the first one?