-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path41_events.js
27 lines (15 loc) · 1.44 KB
/
41_events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// In JavaScript, events are actions or occurrences that happen in the browser, such as a user clicking a button, scrolling the page, or submitting a form.JavaScript provides a way to handle these events and execute code in response to them.
// Here are some common events in JavaScript:
// click: Fires when a user clicks an element.
// keydown and keyup: Fire when a user presses and releases a keyboard key.
//load: Fires when a page or an image finishes loading.
//submit: Fires when a user submits a form.
//resize: Fires when the browser window is resized.
//scroll: Fires when the user scrolls the page.
// To handle events in JavaScript, you can use the addEventListener() method, which accepts two parameters: the name of the event you want to handle, and a callback function that will be called when the event occurs.Here is an example:
// const button = document.querySelector("button");
// button.addEventListener("click", function (event) {
// console.log("Button clicked!");
// });
// In the example above, we use the querySelector() method to select a button element, and then use the addEventListener() method to attach a click event handler to the button.When the button is clicked, the callback function is called and the message "Button clicked!" is printed to the console.
// By using events and event handlers, you can create interactive and responsive web applications that respond to user input and provide a better user experience.