Student starter code (30% baseline)
index.html- Main HTML pagescript.js- JavaScript logicstyles.css- Styling and layoutpackage.json- Dependenciessetup.sh- Setup scriptREADME.md- Instructions (below)๐ก Download the ZIP, extract it, and follow the instructions below to get started!
W3 Server-Side Development & Authentication - Lesson 4
By completing this activity, you will:
IMPORTANT: This template provides a 65% pre-built foundation!
Already implemented for you:
on:click={popUp} syntax workingactivity-04-svelte-events/
โโโ README.md # This file
โโโ 01-alert-button/
โ โโโ App.svelte # 100% complete
โ โโโ Button.svelte # 100% complete
โโโ 02-pin-lock/
โ โโโ App.svelte # 60% complete - needs validation logic
โโโ 03-info-update/
โ โโโ App.svelte # 50% complete - needs update function
โโโ 04-clear-button/
โโโ App.svelte # 40% complete - needs clear function + button
Implement the check() function to validate user PIN entry.
File: 02-pin-lock/App.svelte
Success Criteria:
prompt()+ operator!== for comparison (strict inequality)Code Location: Inside the check() function (currently empty)
Hint: You need to create a variable to store the user's input, then use an if/else statement to check if it matches 4321.
Example Pattern:
function check() {
let userInput = +prompt("Enter PIN:");
if (userInput !== correctPIN) {
// Set error message
} else {
// Set success message and change button
}
}
Implement prompts to collect all four pieces of user information.
File: 03-info-update/App.svelte
Success Criteria:
name variableage variablegithubId variablephoneNumber variableCode Location: Inside the update() function (currently empty)
Hint: You need four separate prompt() calls, one for each piece of information. Use descriptive messages like "Please enter your name".
Create a function to reset all user information and add a Clear button.
File: 04-clear-button/App.svelte (same as 03, with additions)
Success Criteria:
clear() function""btn-danger class (red styling)on:click={clear} eventmr-2 class on Update button for spacingCode Location:
<script> tag after update()<div> after the Update buttonHint: The clear function is very simple - just set each variable back to "". The button follows the same pattern as the Update button.
By examining the working code and completing the TODOs, you'll understand:
on:click={functionName} (no parentheses!)function popUp() { ... } for alertsButton.svelte:
<script>
export let name;
export let text;
export let colour;
function popUp() {
alert(text);
}
</script>
<button class='btn {colour} m-1' on:click={popUp}>
{name}
</button>
<style>
@import url("https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css");
</style>
App.svelte:
<script>
import Button from "./Button.svelte";
let btn1 = {
name: 'Button',
text: 'Hello',
colour: 'btn-success'
};
let btn2 = {
name: 'Alert',
text: 'Alert',
colour: 'btn-primary'
};
</script>
<Button {...btn1} />
<Button {...btn2} />
App.svelte:
<script>
let btnName = "Unlock";
let message = "Click on the button to open the locker.";
function check() {
// TODO 1: Implement PIN validation
// 1. Prompt user for PIN number (use +prompt() to convert to number)
// 2. Check if pinNo !== 4321
// 3. If wrong: message = "Wrong pin number!"
// 4. If correct: btnName = "Opened" and message = "Successfully opened the locker!"
}
</script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<button on:click={check} class="btn btn-success">
{btnName}
</button>
<p class="mt-2">{message}</p>
App.svelte:
<script>
let name = '';
let age = '';
let githubId = '';
let phoneNumber = '';
function update() {
// TODO 2: Implement update function
// Prompt for each field and assign to variables:
// name = prompt('Please enter your name');
// (repeat for age, githubId, phoneNumber)
}
</script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<h1 class="text-center text-white bg-dark mb-0 p-2">
Exercise 3
</h1>
<div class='bg-secondary p-5 text-center text-white'>
<p>Name: {name}</p>
<p>Age: {age}</p>
<p>Github ID: {githubId}</p>
<p>Phone Number: {phoneNumber}</p>
<button class='btn btn-success mr-2' on:click={update}>
Update
</button>
</div>
App.svelte (Based on Activity 3):
<script>
let name = '';
let age = '';
let githubId = '';
let phoneNumber = '';
function update() {
name = prompt('Please enter your name');
age = prompt('Please enter your age');
githubId = prompt('Please enter your Github ID');
phoneNumber = prompt('Please enter your phone number');
}
// TODO 3: Add clear() function here
// function clear() {
// name = "";
// age = "";
// githubId = "";
// phoneNumber = "";
// }
</script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<h1 class="text-center text-white bg-dark mb-0 p-2">
Exercise 3
</h1>
<div class='bg-secondary p-5 text-center text-white'>
<p>Name: {name}</p>
<p>Age: {age}</p>
<p>Github ID: {githubId}</p>
<p>Phone Number: {phoneNumber}</p>
<button class='btn btn-success mr-2' on:click={update}>
Update
</button>
<!-- TODO 3: Add Clear button here -->
<!-- <button class='btn btn-danger' on:click={clear}>Clear</button> -->
</div>
Your activity is complete when:
on:click={functionName} syntax (no parentheses)Test these scenarios in Svelte REPL:
Activity 1 - Alert Button:
Activity 2 - PIN Lock:
Activity 3 - Info Update:
Activity 4 - Clear Button:
Each activity is independent - set up in separate Svelte REPL instances:
Activity One: (No changes needed - reference only)
Activity 2: (TODO 1)
Activity 3: (TODO 2)
Activity 4: (TODO 3)
Issue: "Function is not defined" error
Solution: Make sure function is inside <script> tag and spelled correctly
Issue: Click event not working
Solution: Use on:click={functionName} with NO parentheses - {popUp} not {popUp()}
Issue: Props not showing in Button component
Solution: Check you used export let for all props (name, text, colour)
Issue: Alert/Prompt not appearing Solution: Some browsers block pop-ups - check browser settings, or make sure you're in the REPL preview window
Issue: PIN validation always shows wrong
Solution: Make sure you used +prompt() to convert string to number, and !== for comparison
Issue: Variables not updating UI Solution: Svelte is reactive - but make sure you're assigning directly to variables, not properties of objects
Issue: Bootstrap styles not working
Solution: Copy the entire <link> tag for Bootstrap CDN
Ready for more? Try these bonus features:
confirm() before clearing dataon:click|once or on:click|preventDefault../../Activities/Activity 04- Svelte Events.mdxWhen you've completed all four activities:
Remember: Event handling is what makes web apps interactive. You've learned the core pattern that powers every button, form, and interactive element in modern web development!
๐ก Pro Tip: The on:click={functionName} pattern works for all DOM events: on:mouseover, on:keydown, on:submit, etc. Master this pattern, and you can handle any user interaction!