In this course, youβll build apps that respond to user actions (like clicks and typing), update the screen (change text, styles, images), and sometimes play sounds.
To make this easier, we use special helper functions, and a few common coding patterns that appear in many projects.
Keep this sheet handy as you code!
onEvent(id, type, callback)Run code when the user does something.
// Inline style
onEvent("myButton", "click", function() {
console.log("Button clicked!");
});
// Named function style
function handleClick() {
console.log("Button clicked!");
}
onEvent("myButton", "click", handleClick);
setProperty(id, property, value)Change how an element looks or behaves. Uses CSS-style properties.
setProperty("myLabel", "color", "blue");
setProperty("myBox", "backgroundColor", "lightgray");
setProperty("myImage", "visibility", "hidden"); // hide
setProperty("myImage", "visibility", "visible"); // show
setText(id, text)Change the words inside a label, button, or heading.
(Not for inputs β use setValue instead.)
setText("scoreLabel", "Score: 10");
setText("title", "Welcome!");
getText(id)Read the words from a label, button, or heading.
var name = getText("greetingLabel");
console.log("Text says: " + name);
setValue(id, value)Set the contents of an input box.
setValue("nameInput", "Alex");
setValue("searchBox", ""); // clear
getValue(id)Read what the user typed in an input box.
var name = getValue("nameInput");
console.log("You typed: " + name);
setImageURL(id, url)Change the picture in an <img> element.
setImageURL("logo", "logo.png");
setImageURL("bottlePic", "water.png");
playSound(url, loop)Play a sound file.
playSound("success.mp3"); // play once
playSound("background.mp3", true); // loop forever
Package steps together so you can call them by name.
function greet() {
console.log("Hello!");
}
greet();
Using in onEvent:
function sayHi() {
setText("greetingLabel", "Hi there!");
}
onEvent("hiBtn", "click", sayHi);
Call a function to execute its code.
// Define the function
function showMessage() {
setText("outputLabel", "Hello World!");
}
// Invoke (call) the function
showMessage(); // This runs the function
showMessage(); // Call it again
showMessage(); // Call it multiple times
Key Points:
() are required to invoke a functiononEvent - pass function reference (no parentheses) or use inline function// β
CORRECT - pass function reference (no parentheses)
onEvent("myButton", "click", showMessage); // This works perfectly!
// β
ALSO CORRECT - inline function that calls showMessage
onEvent("myButton", "click", function() {
showMessage(); // Call the function when button is clicked
});
+)Join pieces of text together.
var name = "Alex";
var msg = "Hello " + name + "!";
setText("greetingLabel", msg);
Embed variables directly into text with backticks `.
var score = 12;
setText("scoreLabel", `Your score is ${score} points`);
varStore and update values.
var clicks = 0; // start value
clicks = clicks + 1;
Increase (or decrease) a number step by step.
var count = 0;
function addOne() {
count = count + 1;
console.log(count);
}