Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a3f6c26

Browse files
Mikael Wistoftbenna100
authored andcommitted
Added class work week 5
1 parent dc8676b commit a3f6c26

File tree

5 files changed

+128
-19
lines changed

5 files changed

+128
-19
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Class example</title>
6+
</head>
7+
<body>
8+
9+
<input id="button1" type="button" value="Count up" />
10+
<input id="button2" type="button" value="Log in 3 seconds" />
11+
12+
13+
<script src="EventsAndCallbacks.js" ></script>
14+
</body>
15+
</html>
16+
17+
18+
19+
20+
21+
22+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//Events and callbacks
2+
3+
//https://developer.mozilla.org/en-US/docs/Web/API/Event
4+
5+
6+
//Callbacks
7+
8+
//alias: event handler
9+
10+
//callbacks are just functions, that you write.
11+
12+
//Examples
13+
14+
//The function you give to filter, map methods.
15+
//Button is clicked.
16+
//Mouse is moving over a image.
17+
//Some code you wnat to run 3 seconds from now.
18+
19+
20+
//Exercises
21+
22+
// 1. Create a button with title "Count up". When the button is clicked first time, it should first log // out 0. Next time 2, next time 3, and so on.
23+
24+
// 2. Create a button with title "Log in 3 seconds". When it is clicked it should wait 3 seconds and
25+
// then print the text "Log in 3 seconds". Google which built-in function you need for this.
26+
27+
// 3. Create a handler, that prints the x,y coordinate of the mouse event.
28+
29+
30+
31+
//solution 1
32+
33+
//<button id="button1" >Count up</button>
34+
35+
let counter = 0;
36+
37+
document.querySelector("#button1").addEventListener("click",
38+
39+
function(){
40+
41+
console.log(counter);
42+
counter++;
43+
}
44+
45+
);
46+
47+
48+
//solution 2
49+
50+
//<button id="button2" >Log in 3 seconds</button>
51+
52+
document.querySelector("#button2").addEventListener("click",
53+
54+
function(){
55+
56+
const callbackFunction = () => console.log("Log in 3 seconds");
57+
58+
setTimeout(
59+
callbackFunction,
60+
3000
61+
);
62+
63+
}
64+
65+
);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<title>Class example</title>
7+
</head>
8+
9+
<body>
10+
11+
<input id="btn" type="button" value="Display data" />
12+
13+
<div id="display"></div>
14+
</body>
15+
16+
</html>
17+
18+
<script>
19+
const display = document.getElementById("display");
20+
const btn = document.getElementById("btn");
21+
btn.addEventListener("click", getData)
22+
23+
24+
25+
function getData() {
26+
const url = 'https://randomuser.me/api/?results=10';
27+
fetch(url)
28+
.then((resp) => resp.json())
29+
.then(function (data) {
30+
31+
data.results.forEach(function (author) {
32+
33+
display.innerHTML += `The firstname of the author is: ${author.name.first},
34+
and the lastname is: ${author.name.last}<br>`;
35+
})
36+
})
37+
.catch(function (error) {
38+
console.log(error);
39+
});
40+
}
41+
</script>

JavaScript2/Week5/classwork/index.html

Lines changed: 0 additions & 18 deletions
This file was deleted.

JavaScript2/Week5/classwork/main.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)