Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
<link rel="stylesheet" href="style.css">
</head>
<body>

<div class="container">
<h1>ZIP Code Lookup</h1>
<div class="form">
<input type="text" id="zipInput" placeholder="Enter ZIP Code">
<button onclick="fetchData()">Lookup</button>
</div>
<div id="locationDetails" class="location-details"></div>
</div>
<script src="script.js"></script>
</body>
</html>
Expand Down
24 changes: 24 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
async function fetchData() {
const zipCode = document.getElementById('zipInput').value;

try {
const response = await fetch(`https://api.zippopotam.us/us/${zipCode}`);

if (!response.ok){
throw new error("could not fetch resource");
}
const data = await response.json();
displayData(data);
} catch (error) {
console.error('Error fetching data:', error);
}
}

function displayData(data) {
const locationDetailsContainer = document.getElementById('locationDetails');
locationDetailsContainer.innerHTML = `
<h2>${data['post code']}</h2>
<p>${data.country}, ${data.places[0]['place name']}, ${data.places[0].state}</p>
`;
}

41 changes: 41 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
h1 {
text-align: center;
color: #333;
}

.form {
display: flex;
justify-content: center;
margin-bottom: 20px;
}

.input-group {
display: flex;
}

input[type="text"] {
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px 0 0 5px;
width: 70%;
}

button {
padding: 10px 20px;
font-size: 16px;
background-color: #ff7b00;
color: #fff;
border: none;
border-radius: 0 5px 5px 0;
cursor: pointer;
}


/* Styles for location details */
.location-details {
text-align: center;
background-color: #fff;
padding: 20px;
border-radius: 10px;
}