| Level Navigation: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Current Level: 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 |
Add event listeners to your button and input using the onEvent helper function.
onEvent(elementId, eventType, function) syntaxNeed help with event listeners? Check out these snippets:
onEvent examplesfunction() { } syntax for your event handlersInstead of using anonymous functions, you can create named functions for your event handlers:
function clickHandler() {
console.log("Button was clicked!");
setText("output", "Hello from the button!");
}
// Use the named functions with onEvent
onEvent("my-button", "click", clickHandler);
Named functions make your code more readable and reusable, especially when you need to use the same handler for multiple elements.
You can also use the native DOM addEventListener method instead of the helper function:
function clickHandler() {
console.log("Button was clicked!");
setText("output", "Hello from the button!");
}
// Use native addEventListener
document.getElementById("my-button").addEventListener("click", clickHandler);
This approach gives you direct access to the native DOM API and is the standard method used in professional JavaScript development.
| Level Navigation: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Current Level: 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 |