Responding to User Events
Glissez pour afficher le 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.
Tout était clair ?
Merci pour vos commentaires !
Section 1. Chapitre 26
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Section 1. Chapitre 26