Concept Practice: DOM Elements
Concept: Code Execution
Two main phases of execution:
Video Explanation: https://www.youtube.com/watch?v=xckH5s3UuX4
Concept: Code Execution
Two key components:
Concept: Code Execution
A place to store information (data like variables and objects)
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
let x = 2;
let y = 3;
let sum = add(x, y);
let product = multiply(sum, y);
Concept: Code Execution
A place to store information (data like variables and objects)
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
let x = 2;
let y = 3;
let sum = add(x, y);
let product = multiply(sum, y);
x
y
Concept: Code Execution
The way JavaScript keeps track of where we are in the code, which functions were invoked, and which function is currently running
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
let x = 2;
let y = 3;
let sum = add(x, y);
let product = multiply(sum, y);
main
Concept: Code Execution
The way JavaScript keeps track of where we are in the code, which functions were invoked, and which function is currently running
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
let x = 2;
let y = 3;
let sum = add(x, y);
let product = multiply(sum, y);
main
add()
Concept: Code Execution
The way JavaScript keeps track of where we are in the code, which functions were invoked, and which function is currently running
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
let x = 2;
let y = 3;
let sum = add(x, y);
let product = multiply(sum, y);
main
Concept: Code Execution
The way JavaScript keeps track of where we are in the code, which functions were invoked, and which function is currently running
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
let x = 2;
let y = 3;
let sum = add(x, y);
let product = multiply(sum, y);
main
x
y
Concept: Code Execution
The way JavaScript keeps track of where we are in the code, which functions were invoked, and which function is currently running
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
let x = 2;
let y = 3;
let sum = add(x, y);
let product = multiply(sum, y);
main
x
y
sum
Concept: Code Execution
The way JavaScript keeps track of where we are in the code, which functions were invoked, and which function is currently running
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
let x = 2;
let y = 3;
let sum = add(x, y);
let product = multiply(sum, y);
main
multiply()
Concept: Code Execution
The way JavaScript keeps track of where we are in the code, which functions were invoked, and which function is currently running
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
let x = 2;
let y = 3;
let sum = add(x, y);
let product = multiply(sum, y);
main
Concept: Code Execution
The way JavaScript keeps track of where we are in the code, which functions were invoked, and which function is currently running
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
let x = 2;
let y = 3;
let sum = add(x, y);
let product = multiply(sum, y);
main
x
y
sum
Concept: Code Execution
The way JavaScript keeps track of where we are in the code, which functions were invoked, and which function is currently running
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
let x = 2;
let y = 3;
let sum = add(x, y);
let product = multiply(sum, y);
main
x
y
sum
product
Concept Practice: Code Execution
function f1() {
console.log('hello from f1');
}
function f2() {
f1();
console.log('hello from f2');
}
f2();
main
f2()
f1()
Concept Practice: Code Execution
function f1() {
console.log('hello from f1');
}
function f2() {
f1();
console.log('hello from f2');
}
f2();
main
f2()
Concept Practice: Code Execution
function f1() {
console.log('hello from f1');
}
function f2() {
f1();
console.log('hello from f2');
}
f2();
main
Concept: Browser Events
An event is a signal that something has happened in the DOM
The event queue contains all the pending events that are waiting to be processed.
If an event is in the event queue, the JavaScript engine stops the current execution and starts executing the event handler for the event. Once the event handler finishes executing, the JavaScript engine continues with the code where it left off.
Concept: Browser Events
Concept: Browser Events
Common DOM events:
| Mouse Event | Keyboard Events | Window Events | Input Events |
|---|---|---|---|
| click | keydown | scroll | submit |
| mouseover | keyup | resize | focus |
| mouseout | keypress | DOMContentLoaded | blur |
| mousemove | click | ||
| change |
Concept: Browser Events
To listen or subscribe to when an event occurs in the DOM, JavaScript provides the addEventListener method which accepts two arguments:
// Retrieve the element you want to listen on from the DOM
let button = document.getElementById('submit-button');
// Create an event listener on the element
button.addEventListener('click', function(event) {
alert('The button was clicked');
});
Concept: Browser Events
The event object contains information about an event that occurred in the browser, allowing you to access information about the event and manipulate its behavior.
Concept: Browser Events
Here are some commonly used properties of the event object:
type - indicates the type of event that has occurred, such as "click", "keydown", "load", etc
target - indicates the HTML element that triggered the event
clientX and clientY - indicate the horizontal and vertical coordinates of the mouse pointer when the event occurred
keyCode - indicates the numerical value of the key that was pressed during a keyboard event
Concept: Browser Events
Here are two commonly used methods of the event object:
event.preventDefault() - prevents the default behavior of the event from occurring. For example, you can use this method to prevent a link from opening a new page when clicked.
event.stopPropagation() - stops the event from propagating to parent elements. For example, if a click event occurs on a button inside a nav menu, calling this function will prevent the nav menu from also receiving the click event.
Concept Practice: Events