Postcard Generator: Free JavaScript & jQuery Tutorial
Build Interactive Web Applications with JavaScript Events
What You'll Master in This Tutorial
DOM Manipulation
Learn to dynamically change webpage content using JavaScript. You'll modify text, images, and styling based on user interactions.
Event Handling
Implement unobtrusive JavaScript by separating HTML and JavaScript code. Master anonymous functions and event listeners.
Dynamic Content Generation
Build a fully functional postcard generator that creates custom content based on user menu selections.
This exercise includes 52 pre-organized images (50 states plus D.C. and default) with standardized naming conventions that match the option values in your HTML select menu.
Tutorial Learning Path
Setup and File Structure
Navigate the provided files and understand how images are organized with 2-letter state abbreviations matching select menu values.
Menu Input Handling
Learn to capture user selections from dropdown menus using JavaScript event handlers and anonymous functions.
Dynamic Content Updates
Implement real-time postcard generation by updating background images and text content based on user selections.
Code Optimization
Apply unobtrusive JavaScript principles and clean up debugging code for production-ready implementation.
Setup Verification Checklist
Locate files in Desktop > Class Files > yourname-JavaScript jQuery Class
You'll need Chrome DevTools for testing and debugging later
Each image uses 2-letter abbreviation matching select option values
These elements will be dynamically updated by your JavaScript code
The image filenames use standardized 2-letter state abbreviations that directly correspond to the value attributes in your HTML select options, enabling seamless dynamic image loading.
The dropdown menu's options exist as an array-like structure within JavaScript. We can determine which option the user selected by accessing its index position within this array. Add this logging statement to track user selections:
states.onchange = function() {
console.log('state index: ' + states.selectedIndex);
}Select Alabama from the dropdown menu.
The console should display state index: 1. Alabama shows index 1 because arrays are zero-indexed, and the default "Choose a State" option occupies position 0.
Let's extract the actual state name from the selected option. Add this additional logging statement:
states.onchange = function() {
console.log('state index: ' + states.selectedIndex);
console.log('state name: ' + states.options[0].text);
}You'll notice that while the state index updates correctly, state name always shows United States of America. This occurs because we're hardcoding index 0, which always references the first option regardless of user selection.
Replace the hardcoded 0 with the dynamic states.selectedIndex value:
states.onchange = function() {
console.log('state index: ' + states.selectedIndex);
console.log('state name: ' + states.options[states.selectedIndex].text);
}Add this line to construct the image file path based on the selected state's value:
states.onchange = function() {
console.log('state index: ' + states.selectedIndex);
console.log('state name: ' + states.options[states.selectedIndex].text);
console.log('img/' + states.value + '@2x.jpg');
}img/al@2x.jpg in the console, confirming our dynamic path generation works perfectly.Add this line to dynamically update the postcard's background image (keep this as a single line for readability):
states.onchange = function() {
console.log('state index: ' + states.selectedIndex);
console.log('state name: ' + states.options[states.selectedIndex].text);
console.log('img/' + states.value + '@2x.jpg');
document.getElementById('postcard').style.backgroundImage = 'url(img/' + states.value + '@2x.jpg)';
}Add this line to update the heading text with the selected state's name:
document.getElementById('postcard').style.backgroundImage = 'url(img/' + states.value + '.jpg)';
document.getElementById('state-heading').innerHTML = states.options[states.selectedIndex].text;
}
The innerHTML property allows us to modify the text content within HTML elements dynamically—a fundamental technique for creating interactive web applications.
Add this line to update the greeting text, creating the classic postcard format:
document.getElementById('postcard').style.backgroundImage = 'url(img/' + states.value + '.jpg)';
document.getElementById('greeting').innerHTML = 'Greetings from';
document.getElementById('state-heading').innerHTML = states.options[states.selectedIndex].text;Comment out the three console.log() debugging statements by adding // at the beginning of each line:
Most professional code editors support bulk commenting with Cmd–/ (Mac) or CTRL–/ (Windows) when you select multiple lines. Removing or commenting debug code is a standard practice in production-ready applications—it keeps console output clean and prevents potential performance impacts.
Save your final version.
For reference, you can compare your work with our completed solution located in Desktop > Class Files > yourname-JavaScript jQuery Class > Done-Files > Postcard-Generator.
JavaScript Approaches: Obtrusive vs Unobtrusive
| Feature | Obtrusive JavaScript | Unobtrusive JavaScript |
|---|---|---|
| Code Location | Mixed in HTML attributes | Separated in script tags |
| Maintainability | Scattered and hard to find | Centralized and organized |
| Event Handling | onclick='functionName()' | element.onchange = function() |
| Best Practice | Legacy approach | Modern standard |
Dynamic Postcard Generation Process
Capture Menu Selection
Use states.selectedIndex to get the position and states.options[index].text to get the state name from the selected menu option.
Generate Image Path
Construct the image file path using states.value (the 2-letter abbreviation) combined with the img/ directory path and @2x.jpg extension.
Update Postcard Elements
Dynamically change the background image using style.backgroundImage and update text content using innerHTML property.
Clean Up Debug Code
Comment out or remove console.log() statements after testing to create production-ready code.
Always comment out or remove console.log() statements when your code is working correctly. Debug output can slow down production applications and expose unnecessary information.
Key Takeaways
