-
Notifications
You must be signed in to change notification settings - Fork 2
Exercises (Part 2): JavaScript for the Web
In your script.js file:
- Using
let, declare a variable namedmainTitle. - Using
document.querySelector(), select a level 1 heading element (h1) and store it inmainTitle. - Using
console.log(), print out the value of theh1element'sinnerTextproperty. -
add()the picked class to theh1element'sclassList. - NEW: What happens if you assign (
=) a new value to theinnerTextproperty?
Consider the following array:
let warmupArray = [ 'waffle', 'flapjack', 'pancake',
'crêpe', 'hotcake', 'griddle cake', 'blintz' ];- What is the element at index
4? How can we access it using bracket notation? - What is the index of
'waffle'? - What is the index of
'blintz'? How can we express it usingwarmupArray'slengthproperty?
Pre-made CSS classes: is-collapsible, collapsible.
for (initialization; condition; afterthought) { ... }This webpage has a number of section elements that have been marked with the class is-collapsible to signal that they are compatible with this behaviour.
In this exercise, we will use a for loop to prepare our webpage for having collapsible section elements.
-
Open your
script.jsfile and comment out any code from previous exercises. We will be building on our new JavaScript code. -
Select all elements with the
is-collapsibleclass, store the result in a variable, and use aforloop toadd()thecollapsibleclass to each selected element.
For section elements that are collapsible, whether or not they are open or closed is based on the presence (or absence) of the open class on the section element's header element.
In this exercise, we will use event listeners to create usable buttons for the collapsible section elements. Your task is as follows:
- Create a callback function for
addEventListener()that willtoggle()theopenclass on thecurrentTargetof the event it is provided (e.g.,event.currentTarget). - Select all
headerelements that are descendant of elements with thecollapsibleclass and store them in a variable. Hint:_ Descendant selectors follow the pattern.className elementName - Add a
'click'event listener to each of these elements, providing the callback function you created above as the listener function.
- Listeners are added to elements using the
addEventListener()method of an element. For a mouse click listener (aclickevent), this typically looks like:
someElement.addEventListener('click', listenerFunctionName)-
listenerFunctionName— the name of the function that gets run when the specified event is detected (called a callback function). - Function creation:
function functionName (parameterA, parameterB, ...){
// function code
}-
function— JavaScript's keyword for declaring functions. -
functionName— the name of the function that's being created; used to run (call) the function. -
(parameterA, ....)— information that the function needs to run properly. -
{ function code }— code to run when the function is called.
The section that contains the elements needed for next exercise's image slideshow is currently hidden. To prepare your webpage for the next exercise, and as a review of some of the things we've covered, your task is as follows:
- Select the element with ID
'relaxing-images'andremove()thehiddenclass from it. This element contains everything needed for the slideshow feature.
That's it for this exercise! 🎉
The slide show's initial state should display the element with the first image and hide all other elements. To get the slideshow ready for use, your task is as follows:
- Inside this section, the slideshow images are each a list item (
li) element, they are all contained in an element with thecarousel-listclass. - Create a variable named
imgsto save this collection. - Then, except for the first, hide each element using the
hiddenclass.
The selector: '.carousel-list li' matches the appropriate list item elements for the slideshow!
Building on your code from Exercise 9:
- Add a
'click'event listener to.carousel-listthat uses a callback function namedgoToNextSibling. - Create a variable,
currentImg, and store the first slideshow list item element in it. - Then, create the function
goToNextSiblingwhich:- Hides
currentImgusing thehiddenclass. -
remove()s thehiddenclass from thenextElementSiblingofcurrentImg. - Updates (reassigns)
currentImgto itsnextElementSibling.
- Hides
If need be, use the code scaffold below to get started:
let imgs = document.querySelectorAll('.carousel-list li');
// ...rest of exercise 6 code here...
let currentImg = imgs[0]
function goToNextSibling(event) {
// ...your click listener code goes here...
};
document.querySelector('.carousel-list').addEventListener('click', goToNextSibling);Let's put conditional statements to use. Your task is to fill in the else block in the goToNextSibling function, below:
function goToNextSibling(event) {
currentImg.classList.add('hidden');
if (currentImg.nextElementSibling) { // <-- this line has been added for you
currentImg.nextElementSibling.classList.remove('hidden');
currentImg = currentImg.nextElementSibling;
} else { // <-- this line has been added for you
// your code goes here!
}
}First sibling element:
currentImg.parentElement.firstElementChild