Skip to content

Commit 9532041

Browse files
committed
adding question on events
1 parent 25ce8e0 commit 9532041

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Javascript/what-are-events.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Events are "things" that happen to HTML elements. When JavaScript is used in HTML pages, JavaScript can react on these events. Some of the examples of HTML events are,
2+
3+
Web page has finished loading
4+
Input field was changed
5+
Button was clicked
6+
Example: click event for button element
7+
8+
```html
9+
<!DOCTYPE html>
10+
<html>
11+
<head>
12+
<script>
13+
function greeting() {
14+
alert("Hello! Good morning")
15+
}
16+
</script>
17+
</head>
18+
<body>
19+
<button type="button" onclick="greeting()">Click me</button>
20+
</body>
21+
</html>
22+
```
23+
24+
### Event Handlers and Event Listeners
25+
26+
When a user clicks a button or presses a key, an event is fired. These are called a click event or a keypress event, respectively.
27+
28+
An event handler is a JavaScript function that runs when an event fires.
29+
30+
An event listener attaches a responsive interface to an element, which allows that particular element to wait and “listen” for the given event to fire.
31+
32+
There are three ways to assign events to elements:
33+
34+
- Inline event handlers
35+
- Event handler properties
36+
- Event listeners
37+
38+
### How events work
39+
40+
When events happen to an HTML element in a web page, it checks to see if any event handlers are attached to it. If the answer is yes, it calls them in respective order, while sending along references and further information for each event that occurred. The event handlers then act upon the event.
41+
42+
There are two types of event order: event capturing and event bubbling.
43+
44+
Event capturing starts with the outer most element in the DOM and works inwards to the HTML element the event took place on and then out again. For example, a click in a web page would first check the HTML element for onclick event handlers, then the body element, and so on, until it reaches the target of the event.
45+
46+
Event bubbling works in exactly the opposite manner: it begins by checking the target of the event for any attached event handlers, then bubbles up through each respective parent element until it reaches the HTML element.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ A collection of super-popular Interview questions, along with explanations and i
167167
- [How would you compare two objects in JavaScript?](Large-Collection-of-Popular-Problems-with-Solutions/Objects-Master-List-of-Problems-Super-Useful-Daily-Techniques/compare-two-objects.md)
168168
- [Memoize a function](Large-Collection-of-Popular-Problems-with-Solutions/Objects-Master-List-of-Problems-Super-Useful-Daily-Techniques/Memoize-a-function.md)
169169
- [repaint-reflow](Javascript/repaint-reflow.md)
170-
-
170+
- [What are events?](Javascript/what-are-events.md)
171171

172172
[[] Back to top](#table-of-contents-of-this-readme-file)
173173

0 commit comments

Comments
 (0)