Responding to User Events
Scorri per mostrare il menu
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.
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 1. Capitolo 26
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Sezione 1. Capitolo 26