Apply your knowledge to build something amazing!
:information_source: Info Project Details
querySelectorAll()
forEach()
)graph LR
A[Phase 1: Setup] --> B[Phase 2: DOM Selection]
B --> C[Phase 3: Event Handling]
C --> D[Phase 4: Filtering Logic]
D --> E[Phase 5: Testing]
E --> F[Bonus: Extensions]
style A fill:#e1f5fe
style B fill:#b3e5fc
style C fill:#81d4fa
style D fill:#4fc3f7
style E fill:#29b6f6
style F fill:#ffd54f
What you'll do: Open the project and familiarize yourself with the interface
DO NOT DELETE the existing files in the template:
ONLY EDIT the necessary files.
:warning: Common Pitfall #1 Many students accidentally delete important template files! Remember: only edit the
script.js
file unless instructed otherwise. The HTML and CSS files are already set up for you.
Welcome to Pet Haven! :emoji: You're about to build an interactive pet adoption website where visitors can browse adorable animals by category. This project will strengthen your JavaScript skills while creating something meaningful - helping pets find their forever homes!
The website will:
Before proceeding, make sure you've:
index.html
and script.js
filesFollow the instructions:
We want to be able to change category by clicking on the buttons:
tip Pro Tip Take a moment to click around the template first! Notice how the buttons look clickable but nothing happens yet? That's because we haven't added the JavaScript functionality - that's your job!
Take a look at the index.html file and study where these button groups and animal cards are. Which HTML tags do you think are the buttons and animals?
Answer: The <input>
tags with the "select" class are the button groups and the <div>
tags with the "item" class are the animal cards.
:information_source: Info Understanding Bootstrap Radio Buttons The
<input>
tags have special Bootstrap classes that allow us to create the button groups using radio inputs. This is a clever trick - we're using radio buttons styled to look like regular buttons!
You can learn more from the official Bootstrap 4 documentation: https://getbootstrap.com/docs/4.0/components/buttons/#checkbox-and-radio-buttons
Before this phase, ensure you understand:
querySelectorAll()
instead of querySelector()
Let's start by declaring the necessary variables and select using DOM:
:warning: Common Pitfall #2 Make sure you're writing your JavaScript in the
script.js
file, NOT in the HTML file! Also, don't forget the dot (.) before class names in your selectors.
Declare a variable called buttons
and assign the value as the <input>
tags with the select class using the querySelectorAll()
DOM method.
Like this:
// Select all category filter buttons
// The dot (.) means we're selecting by class name
var buttons = document.querySelectorAll(".select");
tip Why querySelectorAll()?
We use querySelectorAll()
because there are multiple buttons (All, Cats, Dogs, etc.). This method returns a NodeList containing all matching elements, which we can loop through later!
Just like the buttons, declare a variable called cards
and assign the value as the <div>
tags with the item class using the querySelectorAll()
DOM method.
// Select all animal cards
// These are the pet profiles we'll show/hide
var cards = document.querySelectorAll(".item");
These cards are the animals that are available for adoption. Each with their name, species and message written in index.html file.
:information_source: Info Quick Check After adding these two lines, you can test if they work by adding
console.log(buttons, cards);
and checking the browser console. You should see NodeLists containing your elements!
Before starting this phase:
forEach()
doesFor each buttons, add a "click" event listener:
:warning: Common Pitfall #3 Remember that
forEach()
takes a function as a parameter. Many students forget the arrow function syntax. The pattern is:array.forEach((item) => { /* code here */ })
Use the .forEach()
method for each buttons.
// Loop through each button
buttons.forEach((button) => {
// We'll add the event listener here
});
Then inside the .forEach()
arrow function, use the addEventListener()
method to add a "click" event.
buttons.forEach((button) => {
button.addEventListener("click", () => {
// The filtering logic will go here
});
});
tip Understanding Arrow Functions
The () => {}
is called an arrow function. It's a shorter way to write functions. Think of it as "when this happens, do this."
Inside the click event arrow function:
Declare a variable called selected
and assign the value as button.name
(this is to get the name of the <input>
tag)
button.addEventListener("click", () => {
// Get which category was clicked
var selected = button.name;
console.log("Selected category:", selected); // Debug helper!
});
Use the .forEach()
method for each cards. Then, for each cards:
button.addEventListener("click", () => {
var selected = button.name;
// Loop through all animal cards
cards.forEach((card) => {
// Filtering logic goes here
});
});
Here's the complete filtering logic with helpful comments:
cards.forEach((card) => {
// Show ALL animals when "all" is selected
if (selected === "all") {
card.style.display = "block";
}
// Show animals that match the selected category
else if (card.classList.contains(selected)) {
card.style.display = "block";
}
// Hide animals that don't match
else {
card.style.display = "none";
}
});
:information_source: Info How the Filtering Works
The magic is in classList.contains()
- it checks if an element has a specific class!
Test your filtering:
If you have done it correctly, it should look something like this:
Having trouble? Here are common issues and solutions:
script.js
file?.select
and .item
===
) or two (==
)? Both work, but be consistent!"all"
in lowercasetip Pro Debugging Tip
Add console.log()
statements to see what's happening:
console.log("Button clicked:", selected);
console.log("Card classes:", card.classList);
Final testing checklist:
Ready to take your Pet Haven to the next level? Try these exciting challenges!
Add another category to the button groups and a few more animals to it.
We have All, Cats, Dogs, Reptiles and Others. Add at least another category such as Fish or Birds.
Requirements:
tip HTML Structure for New Animals
<div class="item birds"> <!-- Note the "birds" class! -->
<img src="bird-image-url.jpg" alt="Bird name">
<h3>Tweety</h3>
<p>Loves to sing in the morning!</p>
</div>
Make the filtering smoother with CSS transitions:
.item {
transition: opacity 0.3s ease;
}
Show how many animals are currently displayed:
// Add this after your filtering logic
var visibleCount = document.querySelectorAll('.item[style="display: block;"]').length;
console.log("Showing " + visibleCount + " animals");
For the brave programmers:
You can search for more animal images here:
To upload images in StackBlitz:
:warning: Image Tips
- Keep image file sizes small (under 500KB) for faster loading
- Use consistent image dimensions for a professional look
- Always add meaningful
alt
text for accessibility
You've built an interactive pet adoption website! You've practiced:
querySelectorAll()
addEventListener()
forEach()
Take a moment to appreciate what you've created - you're helping virtual pets find their forever homes! :emoji::heart:
When you have completed your "Pet Haven" project, submit it using the link below:
tip Before You Submit Make sure to test your webpage:
Remember: Every expert was once a beginner. Keep coding, keep learning, and keep building amazing things!