-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvents.js
103 lines (78 loc) · 2.82 KB
/
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//* Events - JavaScript
/* In JavaScript, events are actions or events that occur in the user interface, such as clicks.
mouse, keystrokes, window size changes, etc. You can handle these events using functions called
"event handlers" or "event handlers". Here is an introduction basic how to work with events in
JavaScript: */
//? Html events in JavaScript:
/* You can use Html attributes to assign events directly to Html elements. For example, the event
`onclick` is triggered when an element is clicked: */
//* Html
/*
<button onclick="myFunction()">Click</button>
*/
function myFunction() {
alert("The button was clicked.");
}
//? Events in JavaScript:
/* It is more common to assign events using JavaScript to separate the event handling logic from the
Html markup. */
//* Html
/*
<button id="myButton">Click</button>
*/
// Get the element by its ID
let button = document.getElementById("myButton");
// Assign an event to the element
button.addEventListener("click", function() {
alert("The button was clicked.");
});
//? Common Types of Events:
//* - **Click:**
element.addEventListener("click", function() {
//Handle the click event
});
//* - **Change:**
element.addEventListener("change", function() {
// Handle the change event
});
//* - **Keydown, Keypress, Keyup:**
element.addEventListener("keydown", function(event) {
//Handle key pressed event
});
//* - **Mouseover, Mouseout:**
element.addEventListener("mouseover", function() {
//Handle the mouseover event
});
//* - **Submit:**
form.addEventListener("submit", function(event) {
//Handle the form submit event
event.preventDefault(); // Prevent form submission by default
});
//* Event Object:
/* Most event handlers take a parameter, often called `event`, which is an object
`Event`. This object contains information about the event, such as the type of event, the target (item
that triggered the event), etc. */
element.addEventListener("click", function(event) {
console.log("Event type:", event.type);
console.log("Target:", event.target);
});
//* Events Delegation:
/* You can use event delegation to manage events on multiple elements with a single
event handler. This is useful when you have dynamically created items or a list of items
Similar. */
//* html
/*
<ul id="myList">
<li>Element 1</li>
<li>Element 2</li>
<li>Element 3</li>
</ul>
*/
let list = document.getElementById("myList");
list.addEventListener("click", function(event) {
if (event.target.tagName === "LI") {
alert("Element was clicked: " + event.target.textContent);
}
});
/* This is a basic introduction to events in JavaScript. As you go deeper into development
web, you will find more complex situations and more advanced events that you can handle. */