guides/week5-event-driven-apps/helpers.jshelpers.js in your project folderQuick Download: Download helpers.js directly to your project folder
helpers.js in the Week 5 directoryEnsure your project has this file structure:
your-project/
โโโ index.html
โโโ script.js
โโโ helpers.js
โโโ styles.css
โโโ assets/
โโโ images/
Add the helpers.js file to your HTML before your main script.js file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Project</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<!-- Your HTML content here -->
<!-- Include helper functions FIRST -->
<script src="helpers.js"></script>
<!-- Then include your main script -->
<script src="script.js"></script>
</body>
</html>
Important: The order matters! helpers.js must come before script.js.
For an even better development experience, you can also set up VSCode with custom settings:
.vscode folder in your project rootsettings.json inside the .vscode folderindex.html)Note: This step is optional but provides a professional development environment setup with better JavaScript IntelliSense.
Now you can use any of the helper functions in your script.js file:
// Change text content
setText("title", "New Title");
// Change image source
setImageURL("myImage", "new-image.jpg");
// Change CSS properties
setProperty("myDiv", "background-color", "blue");
onEvent Helper// In your script.js file
onEvent("myButton", "click", function() {
console.log("Button clicked!");
setText("output", "Hello from helpers!");
});
playSound Helper when a button is clicked// Play a sound when something happens
onEvent("playSoundButton", "click", function() {
playSound("assets/success.mp3");
// Your other code here
});
Hereโs a complete HTML example that includes all the IDs referenced in the script examples above:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Helper Functions Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<!-- Elements referenced in the script examples -->
<h1 id="title">Original Title</h1>
<div id="myDiv" class="content-box">
<p>This div can have its background color changed</p>
</div>
<img id="myImage" src="default-image.jpg" alt="Example image">
<button id="myButton">Click Me!</button>
<button id="playSoundButton">Play Sound</button>
<div id="output">Output will appear here</div>
<!-- Test elements for the setup verification -->
<button id="testButton">Test Button</button>
<div id="testOutput">Test output will appear here</div>
</div>
<!-- Include helper functions FIRST -->
<script src="helpers.js"></script>
<!-- Then include your main script -->
<script src="script.js"></script>
</body>
</html>
Note: All the IDs in this HTML (title, myDiv, myImage, myButton, playSoundButton, output, testButton, testOutput) match exactly with the IDs used in the JavaScript examples above.
onEvent(elementId, eventType, callback) - Add event listenerssetProperty(elementId, property, value) - Change element propertiessetText(elementId, text) - Change text content of elementsgetText(elementId) - Get text content from elementssetValue(elementId, value) - Set value of input elementsgetValue(elementId) - Get value from input elementssetImageURL(elementId, url) - Set image source URLplaySound(soundPath, loop) - Play audio files with optional loopingThese are the actual helper functions implemented in helpers.js. For native DOM methods like getElementById, querySelector, etc., use the standard browser APIs directly.
helpers.js not loaded or loaded after script.jshelpers.js is in the same folder as index.htmlAdd this to your script.js to test if helpers are working:
// Test if helpers are loaded
console.log("Testing helper functions...");
// Test onEvent
onEvent("testButton", "click", function() {
console.log("onEvent helper working!");
setText("testOutput", "Helpers are working!");
});
console.log("Helper functions loaded successfully!");
If you see โHelper functions loaded successfully!โ in the console, youโre ready to go!
helpers.jsIf youโre still having issues:
helpers.js is loaded before script.jsHappy coding! ๐