JavaScript Simplified / Chapter 74 Exercises

  • Index of exercises
  • Email me

1

2

3

4

5

6

7

8

9

10

Congratulations. You've aced all the exercises for this chapter.


To practice on your own, or to check code you believe shouldn't have been scored as incorrect, go to CodePen.


Are you ready to rate JavaScript Simplified? on Amazon?


Rate it on Amazon.

0,1,2,3,4,5,6,7,8,9

0

0

0

0

To target multiple elements, you use the ________() method. querySelectorAll querySelectorAll
The following code selects all _______ elements. (9 characters) let allSomethings = document.querySelectorAll("p"); paragraph paragraph
Fill in the blank to select all elements with the "normal" class. let allThatShareClass = ______________; document.querySelectorAll(".normal"); document\.querySelectorAll\(•\.normal•\);
Select all divs and assign them to the variable allDivs. let allDivs = document.querySelectorAll("div"); letallDivs=document\.querySelectorAll\(•div•\);
Select all ordered lists and unordered lists and assign them to a variable. Make up the variable name. let allLists = document.querySelectorAll("ol, ul"); let[a-z_$][a-zA-Z0-9_$]*=document\.querySelectorAll\(•ol,ul•\);
Select all th cells in a table and assign them to a variable. Make up the variable name. let allHeadings = document.querySelectorAll("th"); let[a-z_$][a-zA-Z0-9_$]*=document\.querySelectorAll\(•th•\);
All h2 headings have been selected and assigned to the variable h2s. Use a for loop to center them. for (let i = 0; i < h2s.length; i++) {
  h2s[i].setAttribute("align", "center");
}
for\(leti=0;i9Stumph2s\.length;i\+\+\){[\r\n]h2s\[i\]\.setAttribute\(•align•,•center•\);[\r\n]}
In a first statement, select all paragraphs and assign them to a variable. Then code a loop that inserts identical text into each paragraph. Make up the text. let allGraphs = document.querySelectorAll("p");
for (let i = 0; i < allGraphs.length; i++) {
  allGraphs[i].textContent = "Text to come....";
}
letallGraphs=document\.querySelectorAll\(•p•\);[\r\n]for\(leti=0;i9StumpallGraphs\.length;i\+\+\){[\r\n]allGraphs\[i\]\.textContent=•.*•;[\r\n]}

This one may take several tries. Don't blame yourself if you need to go back and review some chapters in the book. Keep at it. A bit of struggle embeds knowledge more deeply.

  1. Click the HTML button to see my markup for three paragraphs and a button.
  2. Click the Result button to see my markup rendered on the page.
  3. Click the JS button.
  4. When Click to center button is clicked, select all the paragraphs and center them. Use a for loop and a named function.
  5. Click the Click to center button.
  6. If you've coded correctly, the paragraphs will center.
  7. For help with this code, see Chapter 74 in the book.

Another challenging exercise. It tests you on several lessons in the book. Review chapters of the book as much as you need to.

  1. Click the HTML button to see my markup for three paragraphs.
  2. Click the Result button to see my markup rendered on the page.
  3. Click the JS button.
  4. Select the paragraphs that have the "canEdit" class and setAttribute() of "contentEditable" to "true". Use a forEach() loop and an arrow function.
  5. Try editing one of the paragraphs to see if it's working.
  6. For help with this code, see Chapter 74 in the book.