Skip to content

Commit 61579c0

Browse files
committed
Dom fundamendtals
1 parent ce8086d commit 61579c0

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

chapter4/app.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
body {
2+
background: black;
3+
}
4+
5+
.soft-body {
6+
background: rgb(228, 238, 196)
7+
}
8+
9+
.bolder{
10+
color: #ababab;
11+
font-weight: bold;
12+
}
13+
14+
.hidden {
15+
display: none;
16+
}

chapter4/app.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
3+
(function(){
4+
function changeBodyColor(){
5+
return new Promise(function(resolve, reject){
6+
var bodyElement = document.getElementsByTagName('body')[0];
7+
bodyElement.className = 'soft-body';
8+
resolve(bodyElement);
9+
})
10+
}
11+
12+
function addSubmitButtonToBody(bodyElement){
13+
return new Promise(function(resolve, reject){
14+
if (bodyElement){
15+
var createdButton = document.createElement('button')
16+
createdButton.innerText = 'Submit';
17+
createdButton.id = 'submit-button'
18+
bodyElement.appendChild(createdButton);
19+
resolve(bodyElement);
20+
} else {
21+
reject('No body available')
22+
}
23+
})
24+
}
25+
26+
27+
function waitUntillButtonIsClicked(){
28+
return new Promise(function(resolve, reject){
29+
var button = document.getElementById('submit-button');
30+
button.addEventListener('click', function(e){
31+
var helloWorldTitle = document.querySelector('h1');
32+
var successTitle = document.querySelector('h2');
33+
34+
helloWorldTitle.className = 'hidden';
35+
successTitle.className = 'bolder';
36+
resolve('Success!')
37+
});
38+
})
39+
}
40+
41+
function showDelayedMessage(message){
42+
return new Promise(function(resolve, reject){
43+
setTimeout(function(){
44+
alert('Hey hey! ' + message)
45+
resolve();
46+
}, 5000);
47+
});
48+
}
49+
50+
changeBodyColor()
51+
.then(function(bodyElement){
52+
return addSubmitButtonToBody(bodyElement);
53+
}).then((res) => {
54+
return waitUntillButtonIsClicked();
55+
})
56+
.then((message) => {
57+
showDelayedMessage(message);
58+
})
59+
.catch(e => {
60+
alert('AAAA' + e)
61+
})
62+
63+
})()

chapter4/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head>
3+
<link rel="stylesheet" type="text/css" href="./app.css"/>
4+
</head>
5+
<body>
6+
<h1>Hello world!</h1>
7+
<h2 class="hidden">Success</h2>
8+
9+
<div class="multiple-click">
10+
<div>Test1</div>
11+
<div>Test2</div>
12+
<div>Test3</div>
13+
<div>Test4</div>
14+
<div>Test5</div>
15+
<div>Test6</div>
16+
<div>Test7</div>
17+
<div>Test8</div>
18+
<div>Test9</div>
19+
</div>
20+
21+
<script src="./app.js"></script>
22+
</body>
23+
</html>

0 commit comments

Comments
 (0)