Responding to User Events
Stryg for at vise menuen
Web pages become interactive when JavaScript responds to user actions like clicks or typing.
To handle user actions, use the addEventListener() method.
const button = document.querySelector(".btn");
button.addEventListener("click", () => {
console.log("Button clicked");
});
When the button is clicked, the function runs.
Common events:
"click": when an element is clicked;"input": when a user types in a field;"submit": when a form is submitted.
Example with user input:
const input = document.querySelector("input");
input.addEventListener("input", () => {
console.log(input.value);
});
JavaScript listens for events and runs code in response, which makes web pages interactive.
Var alt klart?
Tak for dine kommentarer!
Sektion 1. Kapitel 26
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Sektion 1. Kapitel 26