HTML Academy
Summary of “Events in JavaScript”, part 2
Getting to know events25/25
Back to the list of tasks
  • 1. Introduction to events
  • 2. How to add a handler
  • 3. How events are arranged
  • 4. Default actions
  • 5. Please pass the function
  • 6. Hiding the popup
  • 7. Pressing a key
  • 8. Choosing a key
  • 9. With one click
  • 10. Summary of “Events in JavaScript”, part 1
  • 11. First program: “Don’t be shy”
  • 12. Welcome to our photo gallery
  • 13. Click ’em all!
  • 14. Adding an image
  • 15. A bug has crept into the system
  • 16. Scope
  • 17. Global scope
  • 18. Inside out variables
  • 19. Becoming Independent
  • 20. Closures
  • 21. Let’s prepare for school
  • 22. Fixing the gallery
  • 23. Getting to the heart of the matter
  • 24. Summary of “Events in JavaScript”, part 2
  • 25. The Second Program: “Señor Tomato”
List of tasks
  • Sign up
  • Log in

Register to take up challenges

Registration will only take a minute and let you save your study progress. You can register with your email and password or login via social networks.

or
Log in and continue
  • index.html
  • style.css
  • script.js
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Tomato Gallery</title> <link href="gallery-tomato/setting.css" rel="stylesheet"> <link href="style.css" rel="stylesheet"> </head> <body> <section class="gallery"> <h1>Tomato Gallery</h1> <div class="gallery__pictures"> <p class="gallery__picture-full"> <img class="full-picture" src="gallery-tomato/tomato-red-large.jpg" width="450" height="300" alt="Large photo"> </p> <p class="gallery__picture-previews"> <button class="gallery__picture-preview" type="button"> <img src="gallery-tomato/tomato-red.jpg" alt="Red tomato"> </button> <button class="gallery__picture-preview" type="button"> <img src="gallery-tomato/tomato-yellow.jpg" alt="Yellow tomato"> </button> <button class="gallery__picture-preview" type="button"> <img src="gallery-tomato/tomato-strange.jpg" alt="Strange tomato"> </button> </p> </div> </section> <script src="script.js"></script> </body> </html>
CSS
.gallery { display: flex; flex-direction: column; align-items: center; min-width: 555px; } .gallery__pictures { display: flex; } .gallery__picture-full { margin-bottom: 0; } .gallery__picture-full img { display: block; box-shadow: 0 0 5px rgba(0, 1, 1, 0.2); } .gallery__picture-previews { display: flex; width: 90px; margin: 0; margin-right: 15px; flex-direction: column; list-style-type: none; padding: 0; justify-content: space-between; order: -1; } .gallery__picture-preview { position: relative; padding: 0; border: 0; border-radius: 0; background: none; outline: none; top: 0; filter: grayscale(1); opacity: 0.7; transition: top 0.2s, box-shadow 0.2s, filter 0.2s, opacity 0.2s; box-shadow: 0 0 5px rgba(0, 1, 1, 0.2); } .gallery__picture-preview:hover { outline: 3px solid #cccccc; outline-offset: -3px; } .gallery__picture-preview:focus { top: -5px; box-shadow: 0 3px 10px 2px rgba(0, 1, 1, 0.2); filter: none; opacity: 1; } .gallery__picture-preview img { display: block; object-fit: cover; width: 90px; height: 90px; }
JavaScript
var pictures = [ 'gallery-tomato/tomato-red-large.jpg', 'gallery-tomato/tomato-yellow-large.jpg', 'gallery-tomato/tomato-strange-large.jpg' ]; /* Specification Meow! We need to update a gallery of product photos. The gallery has thumbnails (elements with the 'gallery__picture-preview' class) and a large image ('full-picture' class). The large photo should change after you click on the preview. The large image should be the same as the thumbnail, only with larger dimensions. The paths to full-size photos can be found in the 'pictures' array. The order of the elements in the array is the same as the order of the thumbnails in the markup. */

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Click inside the mini browser to put the focus in this window.

100%
Console
var pictures = [ 'gallery-tomato/tomato-red-large.jpg', 'gallery-tomato/tomato-yellow-large.jpg', 'gallery-tomato/tomato-strange-large.jpg' ]; var thumbnails = document.querySelectorAll('.gallery__picture-preview'); var fullPhoto = document.querySelector('.full-picture'); var addThumbnailClickHandler = function (preview, picture) { preview.addEventListener('click', function () { fullPhoto.src = picture; }); }; for (var i = 0; i < thumbnails.length; i++) { addThumbnailClickHandler(thumbnails[i], pictures[i]); }

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.