Skip to content

Commit 4629b26

Browse files
author
PB Sweep
committed
Add DOM_fundamentals.md
1 parent c97df3e commit 4629b26

File tree

5 files changed

+251
-0
lines changed

5 files changed

+251
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"arrowParens": "avoid"
4+
}

05-Guess-My-Number/working/index.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
7+
<link rel="stylesheet" href="style.css" />
8+
<title>Guess My Number!</title>
9+
</head>
10+
<body>
11+
<header>
12+
<h1>Guess My Number!</h1>
13+
<p class="between">(Between 1 and 20)</p>
14+
<button class="btn again">Again!</button>
15+
<div class="number">?</div>
16+
</header>
17+
<main>
18+
<section class="left">
19+
<input type="number" class="guess" />
20+
<button class="btn check">Check!</button>
21+
</section>
22+
<section class="right">
23+
<p class="message">Start guessing...</p>
24+
<p class="label-score">💯 Score: <span class="score">20</span></p>
25+
<p class="label-highscore">
26+
🥇 Highscore: <span class="highscore">0</span>
27+
</p>
28+
</section>
29+
</main>
30+
<script src="script.js"></script>
31+
</body>
32+
</html>

05-Guess-My-Number/working/script.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
/*
4+
// Get textContent from the element
5+
console.log(document.querySelector('.message').textContent);
6+
7+
// Set content
8+
document.querySelector('.message').textContent = '🎉 Correct Number!';
9+
10+
// Get textContent from the element
11+
console.log(document.querySelector('.message').textContent);
12+
13+
document.querySelector('.number').textContent = 13;
14+
document.querySelector('.score').textContent = 10;
15+
16+
document.querySelector('.guess').value = 23;
17+
console.log(document.querySelector('.guess').value);
18+
*/
19+
20+
/* click events */
21+
document.querySelector('.check').addEventListener('click', function () {
22+
const guess = Number(document.querySelector('.guess').value);
23+
console.log(guess)
24+
if (!guess) {
25+
document.querySelector('.message').textContent = 'No Number!';
26+
}
27+
});

05-Guess-My-Number/working/style.css

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
@import url('https://fonts.googleapis.com/css?family=Press+Start+2P&display=swap');
2+
3+
* {
4+
margin: 0;
5+
padding: 0;
6+
box-sizing: inherit;
7+
}
8+
9+
html {
10+
font-size: 62.5%;
11+
box-sizing: border-box;
12+
}
13+
14+
body {
15+
font-family: 'Press Start 2P', sans-serif;
16+
color: #eee;
17+
background-color: #222;
18+
/* background-color: #60b347; */
19+
}
20+
21+
/* LAYOUT */
22+
header {
23+
position: relative;
24+
height: 35vh;
25+
border-bottom: 7px solid #eee;
26+
}
27+
28+
main {
29+
height: 65vh;
30+
color: #eee;
31+
display: flex;
32+
align-items: center;
33+
justify-content: space-around;
34+
}
35+
36+
.left {
37+
width: 52rem;
38+
display: flex;
39+
flex-direction: column;
40+
align-items: center;
41+
}
42+
43+
.right {
44+
width: 52rem;
45+
font-size: 2rem;
46+
}
47+
48+
/* ELEMENTS STYLE */
49+
h1 {
50+
font-size: 4rem;
51+
text-align: center;
52+
position: absolute;
53+
width: 100%;
54+
top: 52%;
55+
left: 50%;
56+
transform: translate(-50%, -50%);
57+
}
58+
59+
.number {
60+
background: #eee;
61+
color: #333;
62+
font-size: 6rem;
63+
width: 15rem;
64+
padding: 3rem 0rem;
65+
text-align: center;
66+
position: absolute;
67+
bottom: 0;
68+
left: 50%;
69+
transform: translate(-50%, 50%);
70+
}
71+
72+
.between {
73+
font-size: 1.4rem;
74+
position: absolute;
75+
top: 2rem;
76+
right: 2rem;
77+
}
78+
79+
.again {
80+
position: absolute;
81+
top: 2rem;
82+
left: 2rem;
83+
}
84+
85+
.guess {
86+
background: none;
87+
border: 4px solid #eee;
88+
font-family: inherit;
89+
color: inherit;
90+
font-size: 5rem;
91+
padding: 2.5rem;
92+
width: 25rem;
93+
text-align: center;
94+
display: block;
95+
margin-bottom: 3rem;
96+
}
97+
98+
.btn {
99+
border: none;
100+
background-color: #eee;
101+
color: #222;
102+
font-size: 2rem;
103+
font-family: inherit;
104+
padding: 2rem 3rem;
105+
cursor: pointer;
106+
}
107+
108+
.btn:hover {
109+
background-color: #ccc;
110+
}
111+
112+
.message {
113+
margin-bottom: 8rem;
114+
height: 3rem;
115+
}
116+
117+
.label-score {
118+
margin-bottom: 2rem;
119+
}

notes/DOM_fundamentals.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# DOM Fundamentals
2+
3+
Document Object Model
4+
5+
Structured representation of HTML documents.
6+
7+
Allows JS to access HTML elements and styles to manipulate them.
8+
9+
DOM is a tree structure.
10+
11+
DOM always starts with `document` object at the top. It's the entry point to the DOM (e.g. `document.querySelector()`)
12+
13+
DOM is not part of JavaScript.
14+
15+
DOM Methods and Properties are part of Web APIs (which we can interact with JS).
16+
17+
There are other Web APIs such as timer, fetch etc.
18+
19+
## Getting and setting values
20+
21+
### Getting text values
22+
23+
```js
24+
// Gets the text context of 'message' class and logs to console
25+
console.log(document.querySelector('.message').textContent);
26+
```
27+
28+
```js
29+
// Gets the value of an input field with class 'guess'
30+
console.log(document.querySelector('.guess').value);
31+
```
32+
33+
### Setting text values
34+
35+
```js
36+
// Sets the content of 'score' class to 10
37+
document.querySelector('.score').textContent = 10;
38+
```
39+
40+
```js
41+
// Sets the value of an input field with class 'guess' to 20
42+
document.querySelector('.guess').value = 20;
43+
```
44+
45+
### Getting and setting style values
46+
47+
```js
48+
// Sets the background-color of 'body' element to #60b347
49+
// Note: the css property has an equivalent camel case style in JS
50+
document.querySelector('body').style.backgroundColor = '#60b347';
51+
52+
// Sets the width of the 'number' class to 30rem
53+
document.querySelector('.number').style.width = '30rem';
54+
```
55+
56+
## Click events
57+
58+
Say we want to get the value of a text field (class='guess') once a button (class='check') is clicked.
59+
60+
```js
61+
document.querySelector('.check').addEventListener(
62+
'click',
63+
function() {
64+
console.log(document.querySelector('.guess').value)
65+
}
66+
);
67+
```
68+
69+
The first argument of `addEventListener` method is `click`. Second argument is an anonymous function that does something.

0 commit comments

Comments
 (0)