-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (44 loc) · 1.8 KB
/
script.js
File metadata and controls
68 lines (44 loc) · 1.8 KB
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
// comments in javascript start with two slashes
/*
this is a multi-line comment in JavaScript. it's just like CSS.
*/
// This code adds an event listener to the button in the HTML.
theButton = document.querySelector("button");
theButton.addEventListener("click", function() {
document.querySelector("img").style.border = "5px solid red";
document.getElementById("bruh").innerText = "The image has a red border now!";
});
/* this looks really complex, but let's break it down
Rhere are different types of selectors:
- document.querySelector("button") selects the first <button> element in the HTML
- document.getElementById("myId") selects the element with id="myId"
- document.getElementsByClassName("myClass") selects all elements with class="myClass"
Here, we're targeting all button elements.
Rhere are different types of events:
- "click" is an event that happens when the user clicks on an element
- "mouseover" is an event that happens when the user hovers over an element with their mouse
- "keydown" is an event that happens when the user presses a key on their keyboard
- and so many more
We can add event listeners to elements using the addEventListener method.
Then, we provide a function that will run when the event occurs.
In this case, when the button is clicked, an alert box will pop up with a message.
*/
/* reference:
IF STATEMENTS
if (comparison) {
// code to run if comparison is true
} else if (otherComparison) {
// code to run if otherComparison is true
} else {
// code to run if neither comparison is true
}
FOR LOOPS
for (let i = 0; i < K; i++) {
// code to run K times. you can set K directly, or use a variable
}
WHILE LOOPS
while (condition) {
// code to run while condition is true
}
if you want to learn more, go here: https://www.w3schools.com/js/default.asp
*/