Skip to content

Commit 78b6a33

Browse files
committed
Refactor age calculator layout and improve form structure
1 parent 1189fcd commit 78b6a33

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

Sprint-1/prep/Age/myAge.html

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#age{
1515
margin: auto;
1616
width: 50%;
17-
height: 300px;
17+
height: max-content;
1818
border: var(--main-color) 2px solid;
1919
border-radius: 10px;
2020
display: flex;
@@ -24,6 +24,15 @@
2424
margin-top: 20%;
2525
background-image: linear-gradient(45deg, #58dbb4 10%, #138f13 100%);
2626

27+
28+
}
29+
form{
30+
display: flex;
31+
flex-direction: column;
32+
align-items: center;
33+
justify-content: center;
34+
margin-top: 20px;
35+
margin-bottom: 20px;
2736
}
2837
input{
2938
margin: 10px;
@@ -50,14 +59,43 @@
5059
height: 45px;
5160
font-size: 1rem;
5261
}
62+
#result{
63+
margin-bottom: 20px;
64+
display: none;
65+
}
5366
</style>
5467
</head>
5568
<body>
56-
<div id="age">
69+
<div id="age">
70+
<h1>Welcome to Age Calculator</h1>
71+
<p>Please enter your year of birth:</p>
72+
<form id="ageForm">
73+
5774

58-
<h1>Welcome to Age calculator </h1>
59-
<p>please Enter your year of birth:</p>
60-
<input type="number" id="yeateOfBirth" min="1980" max="2025" >
61-
<button id ="btn">calculate</button>
75+
<label for="yearOfBirth">Year of Birth:</label>
76+
<input type="number" id="yearOfBirth" min="1980" max="2025" required>
77+
78+
<button id="btn" type="submit">Calculate</button>
79+
</form>
80+
<div id="result">
81+
<h2>Your Age is: <span id="ageDisplay"></span></h2>
82+
83+
</div>
6284
</div>
85+
86+
87+
<script>
88+
const btn = document.getElementById("btn");
89+
const form = document.getElementById("ageForm");
90+
91+
form.addEventListener("submit", function(event) {
92+
event.preventDefault();
93+
const yearOfBirth= document.getElementById("yearOfBirth").value;
94+
const currentYear= new Date().getFullYear();
95+
const age= currentYear - yearOfBirth;
96+
document.getElementById("result").style.display = "block";
97+
document.getElementById("ageDisplay").textContent = `${age} years old`;
98+
});
99+
100+
</script>
63101
</body>

0 commit comments

Comments
 (0)