Flatbooks, a local bookstore, needs a website to start advertising their selection of books. They have a rough template of how they want the website to look but its up to you to help them envision their dream. You will need to update the existing title on the page and add a new batch of elements for every book they have in their catalog, which they have provided a template of how each book should look like on the page. They have also provided the store name and a list of books saved to an object, use this object and your knowledge of DOM manipulation to complete this challenge.
- Update Existing Element of title
- Create New Elements for each book
- Delete Element
-
Fork and Clone the Repository:
- Go to the provided GitHub repository link.
- Fork the repository to your GitHub account.
- Clone the forked repository to your local machine.
- Open the project in VSCode.
- Run
npm install
to install all necessary dependencies.
-
Update Existing Element
- Update the header of page to match the name of the book store
- Select the DOM element with the id of
header
- Save as a variable called
bookStoreTitle
- Change the textContent of
bookStoreTitle
to the name property of thebookStore
variable
- Select the DOM element with the id of
- Update the header of page to match the name of the book store
-
Create New Element
- Create a new element for each book
- Loop through every book
- Each book is saved in the books property of the
bookStore
variable
- Each book is saved in the books property of the
- Within the loop create and change the following new elements:
- Create an
li
element calledbookContainer
- Create an
h3
element calledbookTitle
- Set
bookTitle
textContent to the title of the book
- Set
- Create an
p
element calledbookAuthor
- Set
bookAuthor
textContent to the author of the book
- Set
- Create an
img
element calledbookImage
- Set
bookImage
src to the image url of the book (Reminder you can change any attribute of an html element including the src in the same way as you would change the textContent)
- Set
- Create an
- Select the element with the id of
book-list
and save it to the variablebookList
- Append elements to html
- Append
bookTitle
,bookAuthor
, andbookImage
elements tobookContainer
element - Append
bookContainer
element tobookList
- Append
- Loop through every book
- Create a new element for each book
-
BONUS CHALLENGE: Delete Element
- Delete the element with id of
delete-this
from the DOM
- Delete the element with id of
We know how to add elements and change their attributes. What if we want to remove an element from a page?
We use removeChild()
, as you might guess, to remove a particular child of an
element:
someElement.removeChild(someChildElement);
Let's take a look at a more complex example:
const ul = document.getElementsByTagName("ul")[0];
const secondChild = ul.querySelector("li:nth-child(2)");
ul.removeChild(secondChild);
Here you can see the power of querySelector()
: we can use it to find the
second li
element of ul
. We then pass that element as the argument to our
removeChild
method, which removes the element from our ul
.
What if we want to remove the whole unordered list (ul
)?
We can just call remove()
on the element itself:
ul.remove();
And it's gone!