-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathprototype_constructor.html
More file actions
54 lines (47 loc) · 1.58 KB
/
Copy pathprototype_constructor.html
File metadata and controls
54 lines (47 loc) · 1.58 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Car Prototype Example</title>
</head>
<body>
<h1>Interactive Car Prototype Example</h1>
<form id="car-form">
<label for="model">Enter car model:</label>
<input type="text" id="model" name="model" required>
<label for="year">Enter car year:</label>
<input type="number" id="year" name="year" required>
<label for="miles">Enter car mileage:</label>
<input type="number" id="miles" name="miles" required>
<button type="submit">Add Car</button>
</form>
<div id="output"></div>
<script>
class Car {
constructor(model, year, miles) {
this.model = model;
this.year = year;
this.miles = miles;
}
}
Car.prototype.toString = function() {
return `${this.model} has done ${this.miles} miles`;
};
const form = document.getElementById('car-form');
const output = document.getElementById('output');
form.addEventListener('submit', (e) => {
e.preventDefault();
const model = document.getElementById('model').value;
const year = parseInt(document.getElementById('year').value);
const miles = parseInt(document.getElementById('miles').value);
const car = new Car(model, year, miles);
const carInfo = car.toString();
const carInfoElement = document.createElement('p');
carInfoElement.textContent = carInfo;
output.appendChild(carInfoElement);
console.log(carInfo);
});
</script>
</body>
</html>