/
classes_cookies.html
51 lines (45 loc) · 1.28 KB
/
classes_cookies.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Cookie Example</title>
</head>
<body>
<h1>Interactive Cookie Example</h1>
<form id="cookie-form">
<label for="flavor">Enter your favorite cookie flavor:</label>
<input type="text" id="flavor" name="flavor" required>
<button type="submit">Show Title</button>
</form>
<div id="output"></div>
<script>
class Cookie {
constructor(flavor) {
this.flavor = flavor;
}
showTitle() {
console.log(`The flavor of this cookie is ${this.flavor}.`);
}
}
class FavoriteCookie extends Cookie {
showTitle() {
super.showTitle();
console.log(`${this.flavor} is amazing.`);
}
}
const form = document.getElementById('cookie-form');
const output = document.getElementById('output');
form.addEventListener('submit', (e) => {
e.preventDefault();
const flavor = document.getElementById('flavor').value;
const myCookie = new FavoriteCookie(flavor);
myCookie.showTitle();
output.innerHTML = `
<p>The flavor of this cookie is ${flavor}.</p>
<p>${flavor} is amazing.</p>
`;
});
</script>
</body>
</html>