Apply your knowledge to build something amazing!
:information_source: Project Overview Difficulty Level: Intermediate
Estimated Time: 2-3 hours
Skills Practiced:
- DOM Manipulation
- Event Handling
- CSS Classes with JavaScript
- Audio Control
- Interactive Animations
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.
Let's create an E-greeting card to send your best wishes to your family and friends, or anyone you care for! :tada:
The idea is that your E-greeting card will flip open or close when you click on it, creating a delightful interactive experience.
graph LR
A[Phase 1: Setup] --> B[Phase 2: Customize HTML]
B --> C[Phase 3: JavaScript Logic]
C --> D[Phase 4: Add Audio]
D --> E[Phase 5: Test & Submit]
style A fill:#e8f5e9
style B fill:#e3f2fd
style C fill:#fff3e0
style D fill:#fce4ec
style E fill:#f3e5f5
Follow the instructions carefully to get your project environment ready:
:bulb: Tip[Getting Started Right] Before you start coding, take a moment to explore the provided files:
index.html
- Contains the card structurestyle.css
- Pre-styled card designscript.js
- Where you'll add your JavaScript magicYou are given index.html and style.css files for this project. You can replace some texts of the index.html below:
Let's personalize your greeting card to make it special!
:warning: Common Pitfall Make sure to only replace the
?
symbols and highlighted text. Don't accidentally delete the HTML tags or class names - they're needed for the card to work properly!
Who is this card for? Replace the ? with the name of the person you want to give the card to.
<h3>To: ?</h3>
What is your message? Replace the content that is highlighted in yellow with the message you want to write in the card.
<p class="card-text p-1 mt-3 flex-fill">
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dolore magnam voluptatem autem vitae error ad eum soluta officiis illo doloribus natus cupiditate in, commodi consectetur optio, at
cumque perspiciatis laborum.<br />
On this special day, I would like to wish you: </p>
What's the special occasion? Replace the ? with the celebration name such as Happy Birthday, Merry Christmas or Happy Deepavali:
<p class="card-title"><b>?</b></p>
This card is from you. Replace the ? with your name:
<p class="card-text align-self-end">From: ?</p>
:bulb: Tip [Make It Your Own] You may use the images provided to you. However, you are encouraged to choose and change the image and style of your E-greeting Card. Personal touches make the best gifts!
Before moving to JavaScript, ensure you have:
?
placeholders with actual contentNow comes the exciting part - making your card interactive! Let's add the flip functionality step by step.
:warning: Common JavaScript Pitfalls
- Typo Alert: Make sure your class names match exactly (
.card
not.Card
)- Script Loading: Ensure your script.js is linked at the bottom of your HTML body
- Variable Names: JavaScript is case-sensitive -
isOpen
is different fromisopen
In the script.js file, declare 2 variables called card
and front
, then query select the .card
element and .front
element using DOM.
// Select the card and front elements from the DOM
const card = document.querySelector(".card");
const front = document.querySelector(".front");
Declare a variable called isOpen
and set the value to false
.
// Keep track of whether the card is open or closed
let isOpen = false;
:bulb: Tip [Understanding State] The
isOpen
variable acts as a "memory" for your card. It remembers whether the card is currently open (true
) or closed (false
). This is called state management in programming!
To make the card flip when clicked, you need to attach addEventListener
to the .card
element.
card.addEventListener('click', function() {
if (isOpen) {
// Card is open, so close it
front.classList.remove("open");
isOpen = false;
} else {
// Card is closed, so open it
front.classList.add("open");
isOpen = true;
}
});
If you have done it correctly, it should look something like this:
Test your card interaction:
If your card isn't flipping, try these troubleshooting steps:
Check Console Errors
Verify Script Loading
{/* Make sure this is at the bottom of your body tag */}
<script src="script.js"></script>
Test Your Selectors
// Add these console.log statements temporarily to debug
console.log("Card element:", card);
console.log("Front element:", front);
Check CSS Class
.open
class definedLet's add a magical touch to your greeting card - music that plays when it opens! :emoji:
The song will play when the card is opened, and pause when the card is closed.
:bulb: Tip [Audio Enhancement] Adding audio makes your card more engaging and memorable. Choose a song that matches the occasion - cheerful for birthdays, peaceful for thank you cards, or festive for holidays!
Set the id of <audio>
element to song. Then insert a <source>
element inside the <audio>
element and set the src to your mp3 file link:
index.html
{/* Place this before the closing </body> tag, before your script tag */}
<audio id="song">
<source src="xxx" type="audio/mp3">
</audio>
The song URL is provided in the assets folder. Just click on the song.mp3 file to copy the URL. Place the song URL inside the src attribute. Feel free to add your own song if you like.
Note: Audio asset management instructions for accessing song files
In your script.js file, add audio control:
// Add this after your existing variables
const song = document.querySelector("#song");
// Update your click event listener
card.addEventListener('click', function() {
if (isOpen) {
// Card is open, so close it
front.classList.remove("open");
song.pause(); // Pause the music
isOpen = false;
} else {
// Card is closed, so open it
front.classList.add("open");
song.play(); // Play the music
isOpen = true;
}
});
:warning: Audio Autoplay Restrictions Modern browsers may block autoplay. Your audio will only play after the user's first interaction (clicking the card). This is normal browser behavior to prevent unwanted noise!
Audio functionality check:
If your audio isn't working:
Check File Path
console.log("Audio element:", song);
console.log("Audio source:", song.src);
Handle Play Promise
// More robust audio playing
song.play().catch(error => {
console.log("Audio play failed:", error);
});
Test Audio Format
Check this link to learn more about .play()
method and .pause()
method:
Almost there! Let's make sure everything works perfectly before you submit. :tada:
Complete this final checklist:
:bulb: Tip [Testing Like a Pro] Open your project in an incognito/private browser window to test it fresh, just like your recipient will see it!
Ready to go beyond? Try these advanced features to make your card extra special:
// Add a confetti effect when the card opens
if (!isOpen) {
// Trigger confetti animation
createConfetti();
}
Create a multi-page card that flips through different messages:
Add controls to let users:
Implement localStorage to save card preferences:
// Save user's customizations
localStorage.setItem('cardMessage', userMessage);
localStorage.setItem('cardTheme', selectedTheme);
Experiment with different card animations:
:warning: Challenge Tips Start with the basic project first! Only attempt challenges after your main card is working perfectly. Each challenge builds on the core concepts you've learned.
When you have completed your "E-Greeting Card" project, submit it using the link below:
Make sure to test your webpage before submitting to ensure all required elements are working properly!
:bulb: Tip [Submission Success Tips]
- Test your project one final time
- Make sure your StackBlitz project is saved
- Copy the correct project URL
- Double-check all features work before submitting
- Be proud of what you've created! :star2: