For Loops: Free JavaScript Tutorial
Master JavaScript For Loops and Interactive Web Components
What You'll Build
Interactive Product Chooser
Create a dynamic interface where users can click color swatches to change product images and visual states.
Efficient Event Handling
Use for loops to attach event listeners to multiple elements without repetitive code.
Dynamic Visual Feedback
Implement selected states and hover effects that respond to user interactions.
Tutorial Structure
Basic For Loop
Learn the syntax and structure by creating a simple counting loop in the browser console
DOM Manipulation
Apply for loops to select multiple elements and attach event listeners programmatically
Dynamic Interaction
Use the 'this' keyword to create context-aware event handlers for each element
This tutorial uses Visual Studio Code and Chrome DevTools. You'll work with two separate folders: 'Loops' for basic concepts and 'Product-Chooser-Loops' for the practical application.
For Loop Components
Initialization
Set up a counter variable, typically 'i', with a starting value like 0.
Condition
Define when the loop should continue running, such as 'i < 11' for counting to 10.
Increment
Specify how the counter changes each iteration, commonly 'i++' to increase by 1.
Instead of 'i++', you can use 'i = i + 1' or 'i += 1' to achieve the same result, but 'i++' is the most concise and commonly used.
getElementById vs querySelectorAll
| Feature | getElementById | querySelectorAll |
|---|---|---|
| Return Type | Single Element | Array of Elements |
| Selector Format | Element ID | CSS Selector |
| Use Case | Target One Element | Target Multiple Elements |
Temporarily disable the problematic line to prevent execution errors:
function changeColor() {
console.log(this);
productPhoto.src = 'img/chair-red.jpg';
// colorButton.classList.add('selected');
Commenting out code during development is a standard debugging practice—it allows you to isolate and resolve issues incrementally.
Open Chrome's Console using Cmd–Opt–J (Mac) or Ctrl–Shift–J (Windows).
Click each button and observe the console output—you'll see the actual DOM element being logged for each click. This demonstrates how this provides access to the specific element that triggered each event. Notice that each button element contains an ID property corresponding to its color—this will be crucial for our image mapping logic.
Refine your logging to extract the specific ID attribute:
console.log(this.id);
Property access using dot notation is a fundamental JavaScript operation, allowing us to extract specific attributes from DOM elements.
Save and reload, then test each button in the console.
The console now displays the color name for each clicked button, confirming our ability to programmatically access element-specific data.
Implement dynamic image path construction using the extracted color ID:
function changeColor() {
console.log(this.id);
productPhoto.src = 'img/chair-' + this.id + '.jpg';
String concatenation builds file paths dynamically, eliminating the need for separate functions or conditional statements for each color option. This approach scales effortlessly as you add new product variations.
Save and reload to test the functionality.
Each button now correctly updates the product image to its corresponding color variant—a significant improvement in user experience and code efficiency.
Restore the visual feedback functionality using the context-aware this reference:
function changeColor() {
console.log(this.id);
productPhoto.src = 'img/chair-' + this.id + '.jpg';
this.classList.add('selected');
}
The classList.add() method provides a clean, modern approach to dynamic CSS class management, superior to direct className manipulation.
Save and reload for testing.
Click the blue button to verify correct image display and border application.
Click the yellow button and observe that while it functions correctly, the previous button retains its selected state.
This reveals the need for state management—clearing previous selections before applying new ones. We'll implement a loop-based solution to reset all button states before applying the new selection.
Add a reset loop to clear all button selections before applying the new state:
function changeColor() {
console.log(this.id);
productPhoto.src = 'img/chair-' + this.id + '.jpg';
for(i = 0; i < colorButtons.length; i++) {
colorButtons[i].classList.remove('selected');
}
this.classList.add('selected');
}
This pattern—clearing all states before setting a new one—is common in UI development and ensures consistent visual feedback regardless of previous user interactions.
Save and reload for comprehensive testing.
All four buttons should now provide complete functionality: appropriate image updates, proper visual state management, and mutually exclusive selection behavior. This represents a fully functional product customization interface.
Remove the diagnostic logging statement:
function changeColor() {
productPhoto.src = 'img/chair-' + this.id + '.jpg';
for(i = 0; i < colorButtons.length; i++) {
colorButtons[i].classList.remove('selected');
}
this.classList.add('selected');
}
Production code should never include console logging statements—they can impact performance and expose implementation details to end users.
In JavaScript, we use the keyword this much the way we use the 'this' in everyday natural language.
This Keyword Context
Global Context
Outside any function, 'this' refers to the global object (window in browsers).
Event Handler Context
Inside an event handler, 'this' refers to the HTML element that triggered the event.
Practical Application
Use 'this.id' to get the ID attribute of the clicked element dynamically.
Click vs Hover Events
| Feature | Click Event | Hover Event |
|---|---|---|
| Event Type | click | mouseover |
| User Action | Requires Click/Tap | Triggers on Hover |
| Mobile Compatibility | Full Support | Falls Back to Tap |
Always remove console.log statements from production code. They're valuable for debugging but should not remain in the final version users will interact with.
Key Takeaways
