Photo Filter Website: Getting the Photos to Filter
Master JavaScript Photo Filtering with jQuery Integration
Core JavaScript Concepts Covered
Data Attributes
Learn to use HTML5 data attributes to store filter categories and selection states for dynamic content management.
Array Manipulation
Master JavaScript array operations including push() method and dynamic array population based on user interactions.
DOM Manipulation
Control element visibility and styling through JavaScript by dynamically showing and hiding photo containers.
This tutorial builds on previous photo gallery work. Ensure you have the Photo-Site-Filter folder from the previous exercise and Chrome browser for DevTools testing.
Setup Process
Open Project Files
Navigate to Desktop > Class Files > yourname-JavaScript jQuery Class and open the Photo-Site-Filter folder in your code editor.
Preview in Browser
Open index.html in Chrome to test the current functionality and prepare for DevTools usage during development.
Test Current State
Click navigation links to confirm they only appear functional without actually filtering photos in the main gallery area.
Filter Categories and Data Values
The data-filter values match the CSS classes assigned to image containers, creating a direct relationship between navigation and content filtering.
Array Implementation Strategy
Function Call Setup
Add populateArray() function call to the existing click event handlers in the for loop around line 174.
Array Declaration
Declare empty selectedArray variable in the grabbing elements section to store user filter choices.
Array Reset Logic
Clear the array at the start of populateArray() function to prevent data duplication on subsequent clicks.
All Button vs Specific Filter Logic
| Feature | All Button Selected | Specific Filters Selected |
|---|---|---|
| Array Population | Cycles through all categories | Only active selections |
| Loop Start Index | Starts at index 1 | Starts at index 1 |
| Selection Criteria | All non-'all' filters | data-selected='yes' only |
| Result Array | [animals, buildings, trees, bw] | User's active choices |
Use Chrome DevTools Console (Cmd-Opt-J on Mac, Ctrl-Shift-J on Windows) to verify array population. Expected outputs: single categories show ['animals'] and multiple selections show ['animals', 'buildings'].
Photo Filtering Strategy
Hide All First
Set all image containers to display: none initially to create a clean slate for filtering operations.
Inline-Block Display
Use inline-block display property to maintain layout structure and prevent container collapsing issues.
Implementation Steps
Variable Declaration
Create imageContainers variable using querySelectorAll('.gallery div') to access all photo containers.
Hide Function
Write hideAllPics() function that loops through containers and sets display to 'none' for each element.
Test Functionality
Temporarily call hideAllPics() in click handlers to verify all images disappear when any button is clicked.
Photo Filtering Process
Hide All Photos
Call hideAllPics() function to set all image containers to display: none as the starting point.
Loop Through Selected Array
Iterate through selectedArray to process each active filter category the user has chosen.
Query Selected Classes
Use document.querySelectorAll('.' + selectedArray[i]) to find all elements matching the current filter class.
Show Matching Photos
Set matching image containers to display: inline-block using nested loop with variable j to avoid conflicts.
When writing loops within loops, use different variables (i for outer loop, j for inner loop) to prevent variable conflicts and ensure proper functionality.
Empty Array Handling
Usability Fix Implementation
Empty Array Detection
Create noFilterSelection() function that checks if selectedArray length equals 0 to detect when no filters are active.
Show All Photos
Loop through imageContainers and set display to inline-block when array is empty to show complete gallery.
Update All Button State
Set allButton data-selected attribute to 'yes' to provide visual feedback that all photos are now showing.
Key Takeaways
