const gallery = document.getElementById('tree-gallery');Explanation:
- We are using
document.getElementById()to select an HTML element with the id of 'tree-gallery'. - This element will serve as the container where we will dynamically add content (the tree information) later on.
- The
constkeyword is used to create a constant variable named gallery, which stores a reference to this DOM element.
trees.forEach(function(tree){
console.log(tree);
});Explanation:
- There is a variable
treeswhich is an array of tree objects. - Each object might have properties like name, imagePath, description, and link.
- We use forEach() to loop through every object (referred to as tree) in the trees array.
- This method allows us to execute a function (defined by the code block inside) on each tree in the array.
const treeDiv = document.createElement('div');
treeDiv.className = 'tree';Explanation:
- For each tree, we create a div element using
document.createElement('div'). - This will hold the individual tree's information (like its name, image, and description).
- We assign the div a class name of 'tree' using className.
- This class could be used in CSS to style each tree’s card uniformly.
const treeName = document.createElement('h2');
treeName.textContent = tree.name;
treeDiv.appendChild(treeName);Explanation:
- We create an
h2element usingdocument.createElement('h2')to represent the name of the tree. - We assign the
tree.name(a property from the tree object in the array) to thetextContentof theh2element. - This will display the name of the tree in the heading.
- Finally, we append the
h2(tree name) totreeDivusingappendChild(). - This means the tree's name is now part of the div we created for this specific tree.
const treeImage = document.createElement('img');
treeImage.src = tree.imagePath;
treeImage.alt = tree.name;
treeDiv.appendChild(treeImage);Explanation:
- We create an
imgelement usingdocument.createElement('img'). - We assign
tree.imagePath(the path to the tree's image) to thesrcproperty of the image, so the browser knows where to get the image. - We also set the
altproperty totree.name, which will be displayed if the image can't load, and it improves accessibility for screen readers. - Lastly, we append the
imgelement totreeDiv, making the image part of this tree's display.
const treeDescription = document.createElement('p');
treeDescription.textContent = `Description: ${tree.description}`;
treeDiv.appendChild(treeDescription);Explanation:
- We create a
p(paragraph) element to hold the description of the tree. - We set its
textContentto the string "Description: " followed bytree.description, which comes from the tree object. This will display a descriptive text about the tree. - Then we append the p element to treeDiv.
What are Template Literals?
- The expression
${tree.description}is an example of template literalsin JavaScript. - Template literals are a way to embed variables or expressions inside a string
- Template literals use backticks (`) instead of regular quotation marks.
- Inside the string, you can use the
${}syntax to insert variables dynamically.
Why Use Template Literals?
- Convenience: They allow you to embed variables or expressions directly inside a string, avoiding the need for concatenation (+ operator)
treeDescription.textContent = "Description: " + tree.description;- Readability: The code is more readable and maintainable compared to concatenation, especially when combining multiple variables or long strings.
const treeLink = document.createElement('a');
treeLink.href = tree.link;
treeLink.textContent = "Learn More";
treeDiv.appendChild(treeLink);Explanation:
- We create an
a(anchor) element, which is an HTML link. - We set the
hrefattribute totree.link, the URL where more information about the tree can be found. - The text of the link is set to "Learn More".
Finally, we append this link to the
treeDiv.
gallery.appendChild(treeDiv);Explanation:
- After assembling all the elements (name, image, description, and link) into
treeDiv, we now append this div to the gallery element (which we selected at the beginning). - This adds the complete block of information about this specific tree to the webpage.