-
Notifications
You must be signed in to change notification settings - Fork 2
Exercises (Part 1): JavaScript for the Web
Note: JavaScript normally loads and runs when a web page loads. All of our JavaScript code will go in 👉 script.js 👈, a file that has already been setup to load when our project loads.
In your code editor, re-open script.js if you closed it. Starting on a new/blank line below the code that's already written:
-
Use the
consoleobject'slog()method,console.log("..."), to log the message"Hello world!". - Open the console at the bottom of the preview pane and verify that your message was logged.
- Modify your code from step 1 to log a new message of your choosing!
console.log('some message');document.querySelector('someSelector');For this exercise, you will use console.log() and document.querySelector()☝ to select specific DOM elements and read their properties.
- Select the
mainelement. What is the value of itschildElementCountproperty? - Select an element with the
darkclass. What is the value of itsidproperty? - Select a
p(paragraph) element that's inside an element with IDrelaxing-images. What is the value of itsinnerTextproperty?
document.querySelector('selector').someProperty = 'value';Use the assignment operator = to change properties of DOM elements.
- Select an
h1element and set itsinnerTextproperty to"Hi! I'm <name> and I'm learning JavaScript!" -
Repeat step 1 for an
h2element, using your own message. Hint: Don't forget the quotation marks ("...")!
-
Bonus 1: Select an
img(image) element inside an element with IDhtml-basicsand set itssrcproperty to"https://picsum.photos/200". -
Bonus 2: For step 1, change the
innerHTMLproperty (instead ofinnerText) and wrap your name in<span class='big'>...</span>or<em>...</em>tags, e.g.,"Hi! I'm <em>name</em>..."
Selector hints:
- Bonus 1 selector:
"#html-basics img"-
Bonus 2 full message (using both
spanandem):
"Hi! I'm <span class='big'>name</span> and I'm learning <em>JavaScript!</em>"Note: manual line breaks can be inserted using \n for innerText and <br> for innerHTML.
Pre-made CSS classes to use: picked, hidden, fun.
document.querySelector('someSelector').classList.add('className');For this exercise, you will use JavaScript to select DOM elements and modify their classList property.
To start, open the tab with your project. Then, select...
- ...an
h1,h2,section, ormainelement from your project's DOM, andadd()the picked class to it. - ...an element with the
hiddenclass andremove()it. - ...the element with ID
using-cssandtoggle()thefunclass on it.
_Finally, for each of the steps above, what happens if you run the exact same code multiple times? (Try it!)
An element's classList property contains a list of its classes.
-
add('classToAdd')— addsclassToAddto theclassList. If it is already there, nothing happens -
remove('classToRemove')— removesclassToRemovefrom theclassList. If it isn't there, nothing happens. -
toggle('classToToggle')— ifclassToToggleis in theclassList, it is removed; ifclassToToggleisn't there, it is added.
document.querySelector('someSelector').classList.add('classToAdd');
document.querySelector('someSelector').classList.remove('classToRemove');
document.querySelector('someSelector').classList.toggle('classToToggle');Pre-made CSS classes to use: hidden, transparent, fun.
In your script.js file, start by commenting out the line of code that was added in the previous exercise. Now:
- Use
letto declare a variable namedhiddenSection. - Use
querySelector()to select an element with thehiddenclass and assign (=) it tohiddenSection. - Using the
hiddenSectionvariable:-
toggle()thehiddenclass -
remove()thetransparentclass -
add()thefunclass
-
Don't forget— add(), remove(), and toggle() are accessed through the classList property!
- Variables are created (declared) using the
letkeyword, followed by another word which beocmes the variable's name. - Variables are start with an
undefinedvalue and are assigned values using the assignment operator,=. - Using a single statement to both declare a variable and assign it a value is called initializing:
let newVariable = valueOfVariable;In your script.js file, first comment out the code from the previous exercise. Now:
- Use
letto declare a variable namedheadings. - Use the
querySelectorAll()method to select allh2elements and assign (=) the returned list of elements to headings. - Using the
headingsvariable, do the following:- a.
log()the number of elements (itslengthproperty) inheadings. - b.
add()the picked class to the third element in the array. - c.
log()the value of theinnerTextproperty of the element atindex 1. - d.
toggle()thehiddenclass on the parent element (use theparentElementproperty) of the element atindex 0.
- a.
An array is an ordered collection of data. A NodeList isn't an array, but it behaves similarly enough for our purposes.
-
document.querySelectorAll('someSelector')— returns a list of all elements matching the pattern defined bysomeSelector -
myArray[index]— retrieves an element frommyArrayin the position specified specified byindexinside the square brackets ([])
Pre-made CSS classes: message, reveal.
In this exercise, we will use a for loop to reveal a HIDDEN MESSAGE on this web page! First, comment out any code from previous exercises. Now:
- The words of the HIDDEN MESSAGE are spread through the page's
sectionelements, but they all have themessageclass. - Use
letto declare a variable namedwords. - Assign
wordsthe result of aquerySelectorAll()that would match all the hidden words. - Using a
forloop,add()the reveal class to each element in yourwordsvariable. - Read the HIDDEN MESSAGE! : )