Weight Converter Tool
Weight Converter Tool
Convert between kilograms, grams, pounds, and ounces
body {
background: linear-gradient(45deg, #f3f4f6, #e9ecef);
font-family: Arial, sans-serif;
}
.card {
border: none;
border-radius: 15px;
max-width: 500px;
margin: auto;
background: #ffffff;
}
#convertBtn {
background: linear-gradient(45deg, #007bff, #0069d9);
border: none;
}
#convertBtn:hover {
background: linear-gradient(45deg, #0056b3, #00408a);
color: #fff;
}
.list-group-item {
font-size: 1.1rem;
color: #495057;
}
document.getElementById("convertBtn").addEventListener("click", () => {
const weight = parseFloat(document.getElementById("weightInput").value);
const unit = document.getElementById("unitSelect").value;
const resultsList = document.getElementById("resultsList");
// Clear previous results
resultsList.innerHTML = "";
if (isNaN(weight) || weight <= 0) {
alert("Please enter a valid weight!");
return;
}
// Conversion logic
const conversions = {
kg: { g: weight * 1000, lb: weight * 2.20462, oz: weight * 35.274 },
g: { kg: weight / 1000, lb: weight * 0.00220462, oz: weight * 0.035274 },
lb: { kg: weight / 2.20462, g: weight / 0.00220462, oz: weight * 16 },
oz: { kg: weight / 35.274, g: weight / 0.035274, lb: weight / 16 },
};
// Populate results
const selectedConversions = conversions[unit];
for (let [key, value] of Object.entries(selectedConversions)) {
const listItem = document.createElement("li");
listItem.className = "list-group-item";
listItem.textContent = `${value.toFixed(2)} ${key}`;
resultsList.appendChild(listItem);
}
});