Photo Filter Website: Creating an Exclusive Filter
Master Advanced JavaScript Photo Filtering Techniques
Key JavaScript Concepts in This Tutorial
Inclusive vs Exclusive Filtering
Learn the difference between additive filters that show images matching any criteria versus exclusive filters requiring all criteria to match.
Dynamic DOM Manipulation
Master techniques for toggling element visibility and managing user interface states through JavaScript event handling.
Query Selector Chaining
Understand how to build complex CSS selector strings dynamically to target specific combinations of elements.
Inclusive vs Exclusive Filter Behavior
| Feature | Inclusive Filter | Exclusive Filter |
|---|---|---|
| Selection Logic | Shows images matching ANY selected category | Shows images matching ALL selected categories |
| Query Method | Separate queries: '.animals', '.buildings' | Chained query: '.animals.buildings' |
| Result Count | More results (additive) | Fewer results (restrictive) |
Setup Requirements
Located in Desktop > Class Files > yourname-JavaScript jQuery Class
We'll be using Chrome DevTools for debugging later
Click category links to see current additive behavior
The tutorial provides pre-written HTML and CSS for the checkbox toggle to save development time. This approach focuses learning on the JavaScript logic rather than markup creation.
Adding the Toggle Checkbox
Copy Provided HTML
Get the checkbox code from snippets/checkbox-toggle.txt file
Insert Before Header Close
Paste the code before the closing header tag around line 22
Test in Browser
Save and reload to see the new checkbox under navigation
Breaking the filtering logic into separate functions (filterInclusive and filterExclusive) follows the single responsibility principle, making code more maintainable and testable.
Using Chrome DevTools for Debugging
Open JavaScript Console
Use Cmd-Opt-J (Mac) or CTRL-Shift-J (Windows) to access console
Inspect Checkbox State
Use console.dir(exclusive) to examine the checked property
Test State Changes
Toggle checkbox and re-examine to see true/false values
Instead of writing 'if(exclusive.checked == true)', JavaScript allows the shorthand 'if(exclusive.checked)' since the condition only fires when true.
Query Selector Approaches
| Feature | Inclusive Method | Exclusive Method |
|---|---|---|
| Selector Format | document.querySelectorAll('.animals') | document.querySelectorAll('.animals.buildings') |
| Logic | Separate queries for each category | Chained classes in single query |
| String Building | Not required | queryString += '.' + selectedArray[i] |
When testing with Animals + Black & White + checkbox checked, you should see three results. Adding 'All' category should show just one photo that matches all four categories.
Event Handler Implementation
Add onchange Event
Attach event listener to checkbox for state changes
Call Filter Functions
Execute populateArray() and filterPhotos() on toggle
Test Toggle Behavior
Verify switching between inclusive and exclusive modes works
Complete working code examples are available in Desktop > Class Files > yourname-JavaScript jQuery Class > Done-Files > Photo-Filter-Exclusive for reference.
Key Takeaways
